Friday, 27 May 2005

Scheduling tasks in Linux using Cron

Cron is a daemon which schedules recurring jobs to be executed at a predefined time and date. It is very easy to schedule a job to be run at a particular time using cron. Cron daemon is typically started at boot time and runs continuously in the background. Cron table files (crontabs) are stored in the /var/spool/cron directory, which is not accessible by non-privileged users. In order to access the current cron table, the crontab command is used as follows :
$ crontab -e
The above command will load the user's crontab in an editor - usually 'vi '- for editing by the user. You can set a different editor by setting the $EDITOR variable to a different value.
The crontab file contains 6 fields which are as follows :
Min Hours day-of-month month day-of-week command-to-run
And the values of these fields can take the following form:
Min - 0-59
Hours - 0-23
day of month - 1-31
month - 1-12 or Jan-Dec
day of week - 0-7 or Sun-Sat
Fields in a crontab may be separated by any number of tabs or spaces. Multiple values may be separated by commas. And a '*' symbol in a field represent all valid values.
Suppose I am logged in as root and want to modify a crontab file of a particular user. Then I use the '-u' switch :
# crontab -u username -e
And to list the crontab,
$ crontab -l
You can remove the crontab using the -r switch:
$ crontab -r
Restrict or allow user access to cron
Using the two files, /etc/cron.allow and /etc/cron.deny, root can allow or restrict a user from using cron. This is how it works. If the file cron.allow exists and your username appears in it, you may use the crontab command. If the cron.allow file does not exist and the file cron.deny does, then you must not be listed in the cron.deny to use the crontab. If neither file exists, the default behavior is to allow all users to schedule jobs with cron.
System crontab files
/etc/crontab - Master crontab file
/etc/cron.d/ - directory containing additional system crontab files.
The syntax of the system crontab file is slightly different from the user crontab file explained above. In the system crontab file, the sixth field is a username which will be used to execute the command in the seventh field.
Below is the listing of my system crontab file - /etc/crontab

# File: /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly


As seen above, run-parts is a shell script which takes one argument, a directory name, and invokes all of the programs in that directory. The directories cron.hourly, cron.daily, cron.weekly and cron.monthly contain executables which are run by the master crontab file /etc/crontab . Thus at 4:02 every morning, all of the executables in the /etc/cron.daily directory will be run as root.
If you view the /etc/cron.daily directory, you can see a lot of executables which are run daily at a predefined time as specified in the /etc/crontab file.
For example, look at the following script :
/etc/cron.daily/tmpwatch - this script is used to clean old files out of specified directories. Useful for keeping the /tmp directory from filling up. Because it resides in the /etc/cron.daily/ directory, it is executed once daily by the system crontab file.

No comments:

Post a Comment