Name

cron - Execute scheduled tasks

Description

Cron is a task that operates once every minute from within the login program while the system is idle (waiting for callers or for a console instruction). It executes commands at specified dates and times according to the schedule in the file $/etc/crontab.

Sample crontab file:

Day ## Month HH MM Command * * * * 09 csh echo "Scanning..."; scan * * * 03 20 csh maint -r * * * 06 30 lpr /hd0/spool/lpr * * * 18 30 lpr /hd0/spool/lpr Sun * * 00 00 boot * * Jul 03 30 csh doublemaint * 1 Jan 00 00 banner Happy New Year!

Each line in crontab, good for one time event, contains six fields of information. The arguments in each field must be correctly placed under the field header, the first line in the file.

The six fields, from left to right, are the day of the week (string), date of the month (integer), month of the year (string), the hour (integer), the minute (integer), and a command with any necessary arguments. Unless a path is explicitly given, commands are assumed to reside in $/bin.

All hour and minute arguments must fill the entire width of their associated field (i.e., “03” for “3”). The date of the month is padded with spaces (see the above example for January 1). If an argument starts with an asterisk (*), that entire argument is treated as a wildcard (it will count as a matched time pattern).

In the above example, the system will “scan” for outgoing mail every 9 minutes on the hour. It will run the “maint” C-Shell script every morning at 3:20. Any queued printer jobs in /hd0/spool/lpr will be printed twice a day at 6:30am and 6:30pm. Every Sunday at midnight, the system will reboot itself. During the entire month of July, the double maintenance C-Shell script is executed at 3:30 in the morning. And on January 1, the system will wish you a Happy New Year.

It is best to put high-frequency tasks before other tasks, as shown in the above example. Also note that if you need to perform two or more tasks at or about the same time, you can have crontab reference a single shell script that will perform all the appropriate duties.

Note that internal shell commands, such as “echo”, and shell scripts must be called using the name of the shell first, as in “csh echo Hello World”. This is because cron assumes the first argument in the command line is a program in the $/bin directory.

Files

$/etc/crontab
task table.

Note

Since this isn’t a true multitasking computer, tasks in crontab will only run when the system is not busy doing something else. Understand that some jobs might not get executed. If one task follows a previous task within a span of a minute or two, they should be invoked from a single shell script, one after the other.

See Also

sleep(C)