cron is a daemon that executes programs for users periodically at predefined times. The cron daemon or service is started by the init process during system initialization. cron comes from chronos, the Greek word for time. cron was authored by Brian Kernighan in Version 7 Unix. The 4th BSD edition cron, written by Paul Vixie and popularly known as the vixie cron, comes with most Linux distributions.
CRONTAB File
cron works on the basis of crontab, or cron table, which is a text file. The crontab files for the users are kept in the directory, /var/spool/cron/crontabs. Each user has a crontab file, named /var/spool/cron/crontabs/userid. These crontab files must not be accessed directly; these files should be accessed via the crontab command described later in this tutorial. A sample crontab file is given below:
# DO NOT EDIT THIS FILE - edit the master and reinstall. # (/tmp/crontab.iwioEm/crontab installed on Tue Sep 6 12:26:32 2011) # (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $) SHELL = /bin/bash PATH = ~/bin:/bin:/usr/bin:/sbin:/usr/sbin 14 * * * * df -h >~/tmp/disk_free_space 27 12 * * * ls -ls ~ >~/tmp/directory_"`date`"
The first three lines are added by the system. The rest is from the user. The comments start with the #
character and the entire line is not processed by cron. Each line of a crontab file specifies either an environment variable setting or a command to be executed. The format of the environment variable setting line is,
name = value
If the value has spaces, it should be put inside quotes. Also, there is no textual substitution of environment variables in the right hand side value. By default, the shell, /bin/sh is used for executing the command. One can set a different shell by assigning the relevant command path to the SHELL variable. Similarly, the default PATH is /usr/bin:/bin. If a different PATH is required, it can be set to the desired value.
Then there are lines for commands to be executed at particular times. These lines have a fixed format. Each line has six fields,
minute hour day-of-month month day-of-week command
The first five blank-separated time fields specify when the command is to be executed. After the time fields, there is the command to be executed. The command extends to the end of the line. The command may have multiple words, giving command name, arguments and redirection operators. The permissible values for the five time related fields are,
Field | Permitted Values |
---|---|
minute | 0 – 59 |
hour | 0 – 23 |
day of month | 1 – 31 |
month | 1 – 12, or the first three characters of month's name |
day of week | 0 – 7 where, 0 and 7 are Sundays, or the first three characters of week day's name |
There is considerable flexibility in specifying the five fields in a crontab file. An asterisk in a field means all values for that field. For example a specification of 0 * * * *
, runs the designated command every hour. Then, one can specify a range in the form min – max. For example, a specification of 0 10-13 * * *
indicates that the command should be executed at 10 AM, 11 AM, 12 noon and 1 PM. You can also specify a step in which the particular field value should increase. For example, if you do not wish the job to run every hour and want the job to jun every alternate hour you can specify a step of 2. So a specification of 0 10-14/2 * * *
runs the job at 10 AM, 12 noon and 2 PM. Any field could be a list of values separated by comma. So a spec of 0,15,30,45 0 * * *
runs the job four times between midnight and forty five minutes past midnight. You can alternatively specify a step of 15 for minutes as, */15 0 * * *
. Days are specified in two ways, by the day of the month and the day of the week. If one of these is * and the other is specified by a value, the execution happens as per the field having the value (and not the *). If both are specified as values (and not *), cron executes the command for both values. That is, a spec of 0 0 15 * 3
, results in execution of the command at midnight on the 15 of every month and also on all Wednesdays.
There are a few special cases when you can replace the five time fields by certain special strings. These are,
String | Description | Equivalent time fields |
---|---|---|
@reboot | Run once at system startup | |
@yearly @annually |
Run once a year | 0 0 1 1 0 |
@monthly | Run once a month | 0 0 1 * * |
@weekly | Run once a week | 0 0 * * 0 |
@daily @midnight |
Run once a day | 0 0 * * * |
@hourly | Run once an hour | 0 * * * * |
CRONTAB command
How does one create a crontab file? And how does one add commands to it for the system to execute at designated times? The answer to these questions is the crontab command. Using the crontab command, one can specify the commands to be executed periodically at certain times by the system. For the first command, a crontab file is created for the user. More commands can be added to it subsequently. The crontab command syntax is,
crontab [ -u user ] file crontab [ -u user ] [ -i ] { -e | -l | -r }
With the crontab command, a user can create his or her crontab file. A user with superuser privileges can create a crontab file for other users by using the -u
option. There are two crontab command formats. In the first case the argument file creates (or replaces, if already exists) the crontab file for the user. If –
is used in place of file, the standard input is taken as the file. In the second format of crontab, the -e
option is most important. crontab -e
fetches the current crontab file and displays it in an editor for modification. After you save the file and quit the editor, the saved file is installed as the crontab. The editor is used as per the value of VISUAL or EDITOR environment variables. If neither of these variables are set, then /usr/bin/editor is used. Regarding other options, -l
prints the relevant crontab on the standard output, -r
removes the current crontab for the user and -i
option in conjunction with -r
ensures that the user is asked for confirmation before the crontab is removed.
/etc/crontab and /etc/cron.d
Apart from the user crontabs mentioned above, there is the /etc/crontab and there are crontabs in the /etc/cron.d directory. These crontabs have an additional user-id field for command lines. After the five time related fields, there is the user-id, followed by the command to be executed. The /etc/crontab is for the system administrator while the crontabs in /etc/cron.d are for the packages installed on the system. A package can put a crontab in /etc/cron.d to schedule some activity like clearing of some file or data structure, as required.
How does cron work?
The cron daemon is started by the init process at the system startup. It scans the /var/spool/cron/crontabs directory and loads the user crontabs into its memory. It reads the /etc/crontab and also the crontabs in /etc/cron.d directory. It executes all the crontab commands that were scheduled to run at that time. cron runs commands at a resolution of 1 minute. So it wakes up every minute and runs all the crontab commands that were scheduled for that time. In addition, it looks at the modification times of the /var/spool/cron/crontabs directory and also the mod time of /etc/crontab and /etc/cron.d. If there is any change, the relevant crontabs are loaded into the memory and the commands in latest crontabs are executed, if scheduled at that time.