Cron/at

From Linux 101, The beginner's guide to all things Linux.

Jump to: navigation, search

Cron is a utility that allows events to be scheduled within Linux. An example of an event may be a script to update a log file or send an email out. In fact as long as it's executable, cron can run it. Cron is useful for scheduling recurring events.

The table that cron uses to schedule events is called the crontab and there is usually a crontab for each user including root. To view the crontab for the user that is currently logged in, you can type crontab -l and to view the crontab for a different user you can type crontab -u user -l replacing user with the actual user account in question. For example -

     crontab -u john -l
     00 09 * * Sat john updatedb
30 00 01 * * root tar czf /root/homebak/backup.tar.gz /home >> /dev/null 2>&1

The crontab fields are exepected in this order:

     MM hh dd mm dow user path

where

     MM = minute (00-59)
hh = hour (00-23)
dd = day of month (01-31)
dow = (0-6 or Sun, Mon, Tue etc)
user = the user account to execute the cron job
path = the path of the executable file to run

The above is an example of a user cron entry. There are also general cron entries that are for use within a non-user context and these are broken down into time periods. Linux uses the following directories for placing non-user cron files.

     /etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

If you view the files in these locations you can see several entries that have been added as part of the Linux installation process.

     [root@aiki cron.daily]# ls -l
total 40
lrwxrwxrwx 1 root root 28 Nov 13 18:03 00-logwatch -> ../log.d/scripts/logwatch.pl
-rwxr-xr-x 1 root root 135 Mar 27 2004 00webalizer
-rwxr-xr-x 1 root root 276 Feb 15 2004 0anacron
-rwxr-xr-x 1 root root 180 Feb 15 2004 logrotate
-rwxr-xr-x 1 root root 418 Mar 31 2004 makewhatis.cron
-rwxr-xr-x 1 root root 1603 May 5 2004 prelink
-rwxr-xr-x 1 root root 104 Apr 16 2004 rpm
-rwxr-xr-x 1 root root 82 Apr 16 2004 slocate.cron
-rwxr-xr-x 1 root root 100 Mar 25 2004 tetex.cron
-rwxr-xr-x 1 root root 193 Feb 15 2004 tmpwatch
-rwxr-xr-x 1 root root 136 May 11 2004 yum.cron
[root@aiki cron.daily]#

Looking at one example file...

     [root@aiki cron.daily]# cat slocate.cron
!/bin/sh
. /etc/updatedb.conf
renice +19 -p $$ >/dev/null 2>&1
/usr/bin/updatedb
[root@aiki cron.daily]#

As you can see, the slocate.cron file that cron runs will execute several scripts to update the 'locate' database for the slocate utility.
Type man cron for more information.

Personal tools