One of the most useful utilities in FreeBSD is
cron. This utility runs in the
background and regularly checks
/etc/crontab
for tasks to execute and
searches /var/cron/tabs
for custom crontab
files. These files are used to schedule tasks which
cron runs at the specified times.
Each entry in a crontab defines a task to run and is known as a
cron job.
Two different types of configuration files are used: the
system crontab, which should not be modified, and user crontabs,
which can be created and edited as needed. The format used by
these files is documented in crontab(5). The format of the
system crontab, /etc/crontab
includes a
who
column which does not exist in user
crontabs. In the system crontab,
cron runs the command as the user
specified in this column. In a user crontab, all commands run
as the user who created the crontab.
User crontabs allow individual users to schedule their own
tasks. The root
user
can also have a user crontab
which can be
used to schedule tasks that do not exist in the system
crontab
.
Here is a sample entry from the system crontab,
/etc/crontab
:
# /etc/crontab - root's crontab for FreeBSD # # $FreeBSD$ # SHELL=/bin/sh PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin # #minute hour mday month wday who command # */5 * * * * root /usr/libexec/atrun
Lines that begin with the | |
The equals ( | |
This line defines the seven fields used in a system
crontab: | |
This entry defines the values for this cron job. The
Commands can include any number of switches. However, commands which extend to multiple lines need to be broken with the backslash “\” continuation character. |
To create a user crontab, invoke
crontab
in editor mode:
%
crontab -e
This will open the user's crontab using the default text editor. The first time a user runs this command, it will open an empty file. Once a user creates a crontab, this command will open that file for editing.
It is useful to add these lines to the top of the crontab file in order to set the environment variables and to remember the meanings of the fields in the crontab:
SHELL=/bin/sh PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin # Order of crontab fields # minute hour mday month wday command
Then add a line for each command or script to run,
specifying the time to run the command. This example runs the
specified custom Bourne shell script every day at two in the
afternoon. Since the path to the script is not specified in
PATH
, the full path to the script is
given:
0 14 * * * /usr/home/dru/bin/mycustomscript.sh
Before using a custom script, make sure it is executable and test it with the limited set of environment variables set by cron. To replicate the environment that would be used to run the above cron entry, use:
env -i SHELL=/bin/sh PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin HOME=/home/dru
LOGNAME=dru
/usr/home/dru/bin/mycustomscript.sh
The environment set by cron is discussed in crontab(5). Checking that scripts operate correctly in a cron environment is especially important if they include any commands that delete files using wildcards.
When finished editing the crontab, save the file. It will automatically be installed and cron will read the crontab and run its cron jobs at their specified times. To list the cron jobs in a crontab, use this command:
%
crontab -l
0 14 * * * /usr/home/dru/bin/mycustomscript.sh
To remove all of the cron jobs in a user crontab:
%
crontab -r
remove crontab for dru?y
All FreeBSD documents are available for download at https://download.freebsd.org/ftp/doc/
Questions that are not answered by the
documentation may be
sent to <freebsd-questions@FreeBSD.org>.
Send questions about this document to <freebsd-doc@FreeBSD.org>.