UNIX System Administration Handbook - Evi Nemeth [103]
Table 9.1 Crontab time specifications
Each of the time-related fields may contain
• A star, which matches everything
• A single integer, which matches exactly
• Two integers separated by a dash, matching a range of values
• A comma-separated series of integers or ranges, matching any listed value
For example, the time specification
45 10 * * 1-5
means “10:45 a.m., Monday through Friday.” A hint: never put a star in the first field unless you want the command to be run every minute.
There is a potential ambiguity to watch out for with the weekday and day fields. Every day is both a day of the week and a day of the month. If both weekday and day are specified, a day need satisfy only one of the two conditions in order to be selected. For example,
0,30 * 13 * 5
means “every half-hour on Friday, and every half-hour on the 13th of the month,” not “every half-hour on Friday the 13th.”
The command is the sh command line to be executed. It can be any valid shell command and should not be quoted. command is considered to continue to the end of the line and may contain blanks or tabs. The superuser’s crontab can run commands as an arbitrary user by prefacing them with /bin/su username -c. (You will need to put the actual command in quotes.)
See page 40 for more information about su.
Most versions of cron allow the use of percent signs (%) to indicate newlines within the command field. Only the text up to the first percent sign is included in the actual command; the remaining lines are given to the command as standard input.
Here are some examples of legal crontab commands:
echo The time is now `date` > /dev/console
write garth % Hi Garth. % Remember to get a job.
cd /etc; /bin/mail -s "Password file" evi < passwd
And here are some complete examples of crontab entries:
30 2 * * 1 (cd /users/joe/project; make)
This entry will be activated at 2:30 each Monday morning. It will run make in the directory /users/joe/project. An entry like this might be used to start a long compilation at a time when other users would not be using the system. Usually, any output produced by a cron command is mailed to the “owner” of the crontab.3
20 1 * * * find /tmp -atime +3 -exec rm -f {} ';'
This command will run at 1:20 each morning. It removes all files in the /tmp directory that have not been accessed in 3 days.
55 23 * * 0-3,6 /staff/trent/bin/acct-script
This line runs acct-script at 11:55 p.m. every day except Thursdays and Fridays.
9.3 CRONTAB MANAGEMENT
crontab filename installs filename as your crontab, replacing any previous version. crontab -e checks out a copy of your crontab, invokes your editor on it (as specified by the EDITOR environment variable), and then resubmits it to the crontab directory. crontab -l lists the contents of your crontab to standard output, and crontab -r removes it, leaving you with no crontab file at all.
Most systems allow root to supply a username argument so that other users’ crontabs can be viewed or edited. For example, crontab -r jsmith erases the crontab belonging to the user jsmith. Under Red Hat and FreeBSD, which allow both a username and a filename argument in the same command, the username must be prefixed with -u to disambiguate (e.g., crontab -u jsmith crontab.new).
Without command-line arguments, crontab will try to read a crontab from its standard input. If you enter this mode by accident, don’t try to exit with By default, all users can submit crontab files to cron. Two configuration files, usually called cron.allow and cron.deny, allow you to override this policy. These files can be rather hard to find. Check the directories /etc/cron.d, /usr/lib, /usr/lib/cron, and /var/spool/cron. Under FreeBSD, the files are located in /var/cron and are called simply allow and deny. (Table 9.2 on page 163 lists the standard location of cron-related files on our four example systems.) Table 9.2 Cron-related