Configure an action to be scheduled by cron(8)
Author: name contact BSD flavour
Reviewer: Sean Swayze swayze@pcsage.biz FreeBSD/OpenBSD
Reviewer: name contact BSD flavour
Concept
Understand the difference between the
system crontab(((crontab))) and user crontabs. In addition,
be familiar with using the crontab editor, be able
to recognize the time fields seen in a crontab, and understand the
importance of testing scripts before scheduling their execution
through cron(8)
. Recognize that the files
/var/cron/allow
(((/var/cron/allow))) and /var/cron/deny
(((/var/cron/deny)))
can be created to control which users can create their own crontabs.
Introduction
The cron (((cron))) daemon starts at boot time and is always running. Every minute it checks for updated configurations -- called a crontab -- and runs the jobs that match the specified time.
Two examples of scheduled jobs are log rotations and periodic tasks(((periodic tasks))), introduced in section Configure log rotations and section Configure the scripts that run periodically to perform various system maintenance tasks.
For FreeBSD and DragonflyBSD, the system-wide cron table is commonly located at /etc/crontab
(((/etc/crontab))). NetBSD and OpenBSD keep their default system crontab in the /var/cron/tabs/root
file.
And user crontabs are stored under /var/cron/tabs
directory.
A single crontab configuration is generally placed on one text line. Space or tab delimited, it defines the minute, hour, day of month, the month, the day of week and the shell command to execute. A pound sign (#) at the beginning of a line starts a comment. A comment can not be on the same line as a crontab.
An asterisk (*) matches all ranges (first to last) for a time specification.
The /etc/crontab
format also includes the name of the user to run the command as.
The per-user crontabs do not have the user field.
TODO: show a few examples from default cron and show some more examples and explain
""#minute hour mday month wday who command "" ""# rotate log files every hour, if necessary ""0 * * * * root newsyslog "" ""# run weekly maintenance script every Saturday morning and save and email output ""30 3 * * 6 root mask 077; /bin/sh /etc/weekly 2>&1 | tee /var/log/weekly.out | mail -s "`/bin/hostname` weekly output" root
Note if this file was in the per-user crontabs (such as on a NetBSD or OpenBSD system), it would not have the "who" (root) field.
Note: if both the day of month field and the day of week fields are defined, then the job will run on both of these scheduled times. TODO: for example ...
A crontab can also define shell environment variables, for example TODO ....
Using crontab(1)
to edit user cron table
TODO: note about setuid or setgid, note that OpenBSD does it different? Maybe this doesn't matter for this book
Examples
Practice Exercises
(((cron)))
More information
crontab(1), cron(8), crontab(5)