6.6 Configuring the cron Utility

Contributed by Tom Rhodes.

One of the most useful utilities in FreeBSD is cron(8). The cron utility runs in the background and constantly checks the /etc/crontab file. The cron utility also checks the /var/cron/tabs directory, in search of new crontab files. These crontab files store information about specific functions which cron is supposed to perform at certain times.

Let us take a look at the /etc/crontab file:

# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: src/etc/crontab,v 1.32 2002/11/22 16:13:39 tom Exp $
# (1)
#
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin (2)
HOME=/var/log
#
#
#minute hour    mday    month   wday    who command (3)
#
#
*/5 *   *   *   *   root    /usr/libexec/atrun (4)
(1)
Like most FreeBSD configuration files, the # character represents a comment. A comment can be placed in the file as a reminder of what and why a desired action is performed. Comments cannot be on the same line as a command or else they will be interpreted as part of the command; they must be on a new line. Blank lines are ignored.
(2)
First, the environment must be defined. The equals (=) character is used to define any environment settings, as with this example where it is used for the SHELL, PATH, and HOME options. If the shell line is omitted, cron will use the default, which is sh. If the PATH variable is omitted, no default will be used and file locations will need to be absolute. If HOME is omitted, cron will use the invoking users home directory.
(3)
This line defines a total of seven fields. Listed here are the values minute, hour, mday, month, wday, who, and command. These are almost all self explanatory. Minute is the time in minutes the command will be run. Hour is similar to the minute option, just in hours. Mday stands for day of the month. Month is similar to hour and minute, as it designates the month. The wday options stands for day of the week. All these fields must be numeric values, and follow the twenty-four hour clock. The ``who'' field is special, and only exists in the /etc/crontab file. This field specifies which user the command should be run as. When a user installs his or her crontab file, they will not have this option. Finally, the command option is listed. This is the last field, so naturally it should designate the command to be executed.
(4)
This last line will define the values discussed above. Notice here we have a */5 listing, followed by several more * characters. These * characters mean ``first-last'', and can be interpreted as every time. So, judging by this line, its apparent that the atrun command is to be invoked by root every five minutes regardless of what day or month it is. For more information on the atrun, see the atrun(8) manual page.

Commands can have any number of flags passed to them; however, commands which extend to multiple lines need to be broken with the backslash ``\'' continuation character.

This is the basic set up for every crontab file, although there is one thing different about this one. Field number six, where we specified the username, only exists in the system /etc/crontab file. This field should be omitted for individual user crontab files.

6.6.1 Installing a Crontab

To install your freshly written crontab, just use the crontab utility. The most common usage is:

# crontab crontab

There is also an option to list installed crontab files, just pass the -l to crontab and look over the output.

Users who wish to begin their own crontab file from scratch, without the use of a template, the crontab -e option is available. This will invoke the selected editor with an empty file. When the file is saved, it will be automatically installed by the crontab command.

This, and other documents, can be downloaded from ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/.

For questions about FreeBSD, read the documentation before contacting <[email protected]>.
For questions about this documentation, e-mail <[email protected]>.