Essential reading: man 5 crontab

The first and most important thing to remember about crontabs is that there are two types of crontab. There's the system crontab, kept in /etc/crontab (and files in /etc/cron.d/, /etc/cron.daily/, /etc/cron.hourly/ and /etc/cron.weekly/). Then there's the user crontabs, which are edited by running crontab -e and viewed with crontab -l. These have subtly different formats, and if you put the wrong format in the wrong place, things will not work (or at best, not as you expect them to).

System crontab format

Each line in the system crontab contains 7 whitespace-separated fields. These are, in order:

The user field indicates the user to run the command as. The command is taken to the end of the line.

User crontab format

Each line in a user crontab contains ony 6 whitespace-separated fields. These are:

Note that in a user crontab, there is no user field, and all commands are run as the user that owns the crontab. Also note that the root user's user crontab is not the same as the system crontab in /etc/crontab.

How to write crontab time specifications

cron wakes up every minute, and checks the first five fields of each line in every crontab to see if they match the current time (to the nearest minute). If they do, the corresponding command is executed. A line is considered to match if either one of the "day" specifiers matches.

A * in a field matches all possible values of that time.

A number or list of numbers in a field matches only that number or set of numbers.

A */n (where n is a number) matches possible value of the field in steps of n.


It's worth bearing in mind, where you can avoid it, not to run cron jobs exactly on the hour -- especially if you use ntpdate or some other means to automatically correct clock-drift. Often, such things interfere with cron, resulting in the specified job either running later than scheduled, or not at all.

You can't make cron run only on the first Saturday of every month on its own. The time specification 5 9 1-7 * 6 will run on the first 7 days of the month and on every Saturday. The trick here is to use a wrapper script that checks for one or other of the conditions:

5 9 1-7 * * run-on-dow Sat echo "Wake up! It's a [[HantsLUG]] meeting day!"

Where run-on-dow is something like:

if [[ `date|+%a` = $1 ]]; then
    exec "$@"

LinuxHints/CrontabFormat (last edited 2005-06-18 16:25:44 by 81)