Cron Jobs
Understanding Cron jobs
The daemon that corresponds to the clock of a Linux system is referred to as cron. A daemon is started only once; it automatically sleeps when not required and runs perpetually. The cron daemon executes certain tasks at pre-programmed time intervals. Processes that are scheduled to run in this manner are referred to as 'cron jobs'. A system administrator or other users may add new cron jobs and modify or delete existing jobs.
Cron configuration files and programs that make up the scheduled cron jobs are typically located at /usr/bin/ or /usr/sbin. The files that control cron processes are called cron-tables or crontabs. The crontabs are often located in a subdirectory of /var/spool. The name of each crontab file corresponds to the name of the user who has submitted the file. Each user may modify his or her crontab using the crontab utility
Alternatively, some systems include directories with names like cron.daily, cron.weekly, cron.monthly and so on in the /etc folder; any scripts in these folders will correspondingly be executed once every day, week, or month. The crontab file of such systems contains the execution instructions for the scripts in these folders. In some systems, an 'allow' file contains the list of users who are allowed to edit crontabs. Other systems have a 'deny' table that contains the users who do not have access to the crontab. Some systems do not use these files and others do not allow users other than the superuser to create cron jobs.
The Linux cron daemon checks for more jobs every minute. For this reason, the crontab files should never be edited directly using text editors like vi or emacs. The crontab tool should be used to edit cron job configuration.
The crontab utility is used to submit files that contain scheduled jobs to the system and may also be used to list the contents of a crontab. The root user may submit or view jobs on behalf of all other users. The crontab utility does not allow individual job submission; the entire crontab file should be submitted all at once. Most dialects of Linux allow a -e option that the user may use to edit the current list of cron jobs. A -u option adds a cron tab for the specified username. The following command illustrates how the root user may set up a crontab for LinuxUser:
# crontab -u LinuxUser -e
A new line should be added to this table to add a new cronjob. Once the 'crontab' utility has been used to make changes to a crontab, the original crontab is always removed. Each crontab entry consists of six fields. The first five space-separated fields contain the time interval between execution; the sixth field contains the name of the user and the seventh field contains the command or the name of the script to execute.
| Field # | Time Interval | Units |
| 1 | minutes | 0-59 |
| 2 | hour | 0-23 (O- midnight) |
| 3 | day of the month | 1-31 |
| 4 | month of the year | 1-12 |
| 5 | day of the week | 0-6 (0 - Sunday) |
An asterisk (*) may be used in lieu of a number to include all values of a time interval. For example, to indicate that a weekly job should run for all months, a '*' should be entered in the month field. The following cron job will be executed fifteen minutes past every hour:
15 * * * * root echo "It is now fifteen minutes past the hour!"
Following is an example of a crontab file from a system that uses the /etc/cron.time_interval type directories. Comments are allowed in crontab files; they should be preceded by a hash (#) symbol.
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/usr
# The entries follow: 15 * * * * root run-parts /etc/cron.hourly 30 3 * * * root run-parts /etc/cron.daily 00 4 * * 6 root run-parts /etc/cron.weekly 30 4 1 * * root run-parts /etc/cron.monthly
SHELL is the shell that the crontab runs in. All the directories that contain commands used in the crontab should be specified in PATH. The value of MAILTO indicates the user to whom status, error messages, and other output of the jobs will be sent via e-mail. HOME indicates cron's home directory. The actual cron table follows these variables. The run-parts command is available on many distributions; it simply runs all the scripts in a directory. The scripts in the cron.hourly directory are scheduled to run fifteen minutes past every hour; cron.daily scripts are scheduled to run at 3:30 am every morning. The weekly jobs are scheduled to run at 4:00 am Saturday morning; the monthly jobs are scheduled to run at 4:30 am on the first day of each month.
A job that contains both a day of month and day of week will be executed for both values; however, it will not be executed twice when the specified day of month and day of week match. A crontab entry may contain comma separated values, intervals, or step values to allow repetition within the interval. A three-letter abbreviation may also be used instead of a number for the day of week and month:
30 23 1 * 6 root echo "11:30pm on the first of the month and Saturday" 00,30 * * * * root echo "Once every half-hour" 30 3 */2 * * root echo "3:30 am once every two days" 30 3 * * 1-5 root echo "3:30 am once each weekday (mon - fri)" 00 5 1 jan * root echo "5 am on the 1st of January" 00 5 * * sat root echo "5 am every Saturday"
Cron is not exact - it is simply synchronized to each minute. Jobs may be executed a minute or two behind schedule on busy systems. When the system is very busy, a cycle or two of cron jobs that are run every minute may be skipped entirely. Scheduling cron jobs is easy. Making sure that the cron jobs will not end up hogging system resources or affect other users is a lot harder and the skill to do so only comes with experience.