The cron facility provides a powerful, minute-resolution process scheduler. If a process needs to run repeatedly without human intervention, an entry in the crontab file can accommodate most schedules. There are simple rules for modifying the crontab entries that must be followed.
To edit the crontab file, the crontab program must be used. The actual crontab files should not be edited directly because the contents are cached and changes will not take effect until the crond process is restarted. Using the crontab program to edit the crontabs will update the cache when the file is changed. To edit the current user’s crontab file, use the following:
crontab -e
The -e option tells the program to edit a copy of the user’s crontab file. The EDITOR environment variable is referenced to determine which editor to use (default is ed). To set this environment variable, see recipes for ksh and sh.
The superuser can edit a specific user’s crontab by adding the username at the end of this command. The processes run from a user’s crontab will be run as that user. Be careful with commands in root’s crontab because these will run as root and could cause problems. If shell scripts are run from root’s crontab, make sure their file permissions do not allow modification by anyone but root.
The syntax of crontab is simple. Each line represents a single scheduled task. The first five fields represent timing information and everything following is interpreted as the command to schedule. The timing fields in order are as follows:
minutes – 0-59
hours – 0-23
days of month – 1-31
months of year – 1-12
days of week – 0-6 (Sunday-Saturday)
A variety of options work for each field. An asterisk (*) indicates all possible occurrences for that field. A number sets that single occurrence. Two numbers separated by a – indicates a range of values, and numbers separated by a comma indicate a list of occurrences.
Here are several examples:
15 * * * * logcheck
Runs a command called ‘logcheck’ every 15 minutes of every day.
0,15,30,45 8-17 * * 1-5 dobackup
Runs dobackup every 15 minutes (i.e., 8:00, 8:15, 8:30, and 8:45) during business hours (from 8:00 to 17:00) during business days (Monday-Friday).