4.6 Scheduling Recurring Tasks with Cron

4.6 Scheduling Recurring Tasks with Cron

Unix provides a service called cron that runs programs repeatedly on a fixed schedule. Most experienced administrators consider cron to be vital to the system because it can perform automatic system maintenance. For example, cron runs log file rotation utilities. You should know about cron not only in the interest of knowing about everything that runs on your system, but also because it is just plain useful.

You can run any program you like with cron , choosing the times that suit you. A program running through cron is called a cron job . To install a cron job, you need to create an entry line in your crontab file , usually with the crontab command. Here is a typical crontab file entry that runs a command daily at 9:15 AM:

 15 09 * * * /home/juser/bin/spmake 

The five whitespace-delimited fields at the beginning specify the schedule time (see also Figure 4-3):

  • Minute (0 “59)

  • Hour (0 “23)

  • Day of month (1 “31)

  • Month (1 “12)

  • Day of week (0 “7; 0 and 7 are Sunday)

click to expand
Figure 4-3: An entry in the crontab file.

The preceding example says to run spmake daily, because the day of month, month, and day of week fields are all * (the * character matches all possible values, meaning every day of the month, every month, or every day of the week). If you want to run the job only on the 14th of each month, use this crontab line:

 15 09 14 * * /home/juser/bin/spmake 

You may select more than one time for each field. For example, if you would like the program to run on the 5th as well as the 14th, use 5,14 in the third field:

 15 09 5,14 * * /home/juser/bin/spmake 

The remainder of the line is the command to run. When cron runs the job, the command may generate standard output or error or exit abnormally. If that happens, cron mails this information to you. You may want to redirect the output to /dev/null or some log file if you find this annoying.

The crontab(5) manual page is a full reference for the cron file format. However, there is also a crontab(1) manual page for the installation utility, so you'll need to use man 5 crontab to get to the right one.

4.6.1 Installing Crontab Files

Each user can have their own crontab file. The system usually stores all crontabs in /var/spool/cron/crontabs . Normal users can't write to this directory. Instead, the crontab command installs , lists, edits, and removes a user's crontab.

The easiest way to install a crontab is to put your crontab entries into a file, then use crontab file to install file as your current crontab. The crontab command checks the file format to make sure that you haven't screwed anything up. If you want to list your cron jobs, run crontab -l . To remove the crontab, use crontab -r .

Using temporary files to create and manipulate your crontab can be a little messy, so if you want to edit and install your crontab in one motion, run crontab -e . If you make a mistake, crontab tells you where the mistake is and asks if you would like to try editing again.

4.6.2 System Crontab Files

Rather than use the superuser's crontab for scheduling recurring system tasks, Linux distributions normally have an /etc/crontab file. Do not use the crontab command to edit this file; it has a slightly different format with one more field ” the user that should run the job. Here is an example for a job to run at 6:42 AM, running as the superuser ( root ):

 42 6 * * * root /usr/local/bin/cleansystem > /dev/null 2>&1 

Some distributions also use the /etc/cron.d directory to store system crontab files. These files may have any name , but they have the same format as /etc/crontab .

How Linux Works
How Linux Works: What Every Superuser Should Know
ISBN: 1593270356
EAN: 2147483647
Year: 2004
Pages: 189
Authors: Brian Ward

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net