Cover V01, I04
Article
Listing 1
Listing 2
Listing 3
Listing 4
Sidebar 1

nov92.tar


Sidebar: Using cron

The cron program is a system daemon that runs every minute that a UNIX system is up and running normally. It "wakes up" each minute and checks the crontab file for work to be done. The actual implementation (wake up each minute vs. sleep until the next time work needs to be done) varies from one version of UNIX to another, but the behavior is the same: you can specify a command to be run at any minute of the day.

cron is usually used by the super user to specify work to be done automatically. Many newer versions of UNIX also implement a user crontab file that allows users to define their own commands to be run at specified times.

The original system crontab file was (and in some older versions of UNIX, still is) /usr/lib/crontab. To work with cron, you simply edited the crontab file while you were acting as the root user. cron either sensed the file had been modified and re-read the information or required that a hangup signal be sent to the cron process via the kill command to tell cron to re-read the crontab file.

Most newer versions of UNIX -- including SVR4 (System V, Release 4), SunOS (from Sun Microsystems), and AIX (from IBM) -- include the "new and improved" interface to the crontab file. Now, rather than editing the crontab file directly, you use the crontab -e command (crontab -l will list it). By allowing the crontab command to manage the files and directories itself, you can also permit users to create and manage their own crontab files (this capability becomes a privilege to be allowed or disallowed for individual users). User management of cron is beyond the scope of this article, however. For more information, consult the man pages on your system for cron and crontab (in sections 1, 5, and 8).

The crontab File

cron may operate differently now, but the format of the file remains the same. There are six fields in a crontab file: the first five fields define when a command is to be run, and the sixth field is the command itself. These fields are separated by white space (one or more spaces or tabs) but the sixth field may contain white space. The first five fields are made up of one or more integers separated by commas (to mean "or") or dashes (to mean "through") or an asterisk (to mean "match any value"). When a time that matches the designation for a command is reached, that command is executed. The command can be a shell script or a program. Note that in the case of the system crontab, this program is executed with the privileges of the root user, so great care should be taken to make sure it does only what you intend.

The first five fields have the following meanings:

field # meaning
1 minutes past the hour (0-59)
2 hour of the day (0-23)
3 day of the month (1-31)
4 month of the year (1-12)
5 day of the week (0-6, Sun=0, Mon=1, ... Sat=6; or, for SunOS and other BSD-derived versions of UNIX, 1-7, Mon=1, Sun=7).

An example crontab file might look like this:

10 2 * * * /usr/local/lib/cron.daily
5 1 * * 1 /usr/local/lib/cron.weekly
1 0 1 * * /usr/local/lib/cron.monthly
15 * * * * /usr/local/lib/cron.hourly
20,50 8-17 * * 1-5 /usr/local/status
0, 7 * * 1 /usr/ucb/rdist \
-f /sysadmin/distfile root

The first entry will run the program /usr/local/lib/cron.daily at 2:10 A.M. every day. The second entry will run the program /usr/local/lib/cron.weekly at 1:05 A.M. every Monday morning. The third entry will run the program /usr/local/lib/cron.monthly at one minute after midnight on the first of each month. The fourth entry will run the program /usr/local/lib/cron.hourly at 15 minutes past every hour. The fifth entry will run the program /usr/local/status every 30 minutes between 8:20 A.M. and 5:50 P.M. (inclusive) on weekdays. The sixth entry is an example from "rdist to the Rescue!" (see p. 18). It runs the rdist program on the distribution file /sysadmin/distfile and distributes the files in the "root" package there at 7:00 A.M. each Monday.

Take care that you space out execution of various commands over time (i.e., don't start everything at zero minutes past the hour), because if you wind up starting several commands at the same moment, you may cause your system to "hiccup" slightly (i.e., seem to freeze up momentarily while several new processes are started).