crontab

scheduling

crontab [ -u user ] file       # replaces existing crontab
crontab [ -u user ] { -l | -r | -e }

-l list
-r remove
-e edit using $VISUAL or $EDITOR

-u user whose crontab is to be used. Do not su and run crontab; use -u

Avoid using crontab -e. Save the current version in archive by using:

export yymmdd=`date +"%y%m%d"`
crontab -l > archive/crontab.$yymmdd; edit archive/crontab.$yymmdd ; crontab archive/crontab.$yymmdd

Blank lines, leading spaces and tabs are ignored, # lines are comments
Comments are not allowed on the same line as commands or environment variable settings.
Time Zone is server's

Each line is either a CRON COMMAND or an ENVIRONMENT SETTING.

cron configuration

command is executed when any of the two day fields (day_of_month and month or day_of_week) and the current time match.

For example: 30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

Non-existant times, such as "missing hours" not match, No validation is done on ANY field, including testing for legal time, dates or legal characters.


ENVIRONMENT SETTING

is of the form, name = value

Spaces around the = are optional, embedded spaces in value will be part of the value. The value string may be placed in apostrophes or quotes (single ) to preserve blanks.

$SHELL is set to /bin/sh
$LOGNAME and $HOME are set from /etc/passwd (may be USER on BSD systems)
$HOME and $SHELL may be overridden by settings in crontab.

STDOUT from the command is mailed to the owner of the crontab.
MAILTO to send it to another address, for example me@example.com, if null no mail will be sent.

Example cron file

       # use /bin/sh to run commands, no matter what /etc/passwd says
       SHELL=/bin/sh
       # mail std output to 'paul@otherhost.com', no matter whose crontab this is
       MAILTO=paul@otherhost.com
       #
       # five minutes after midnight, every day
       5   0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
       # at 2:15pm on the first of every month 
       15 14 1 * *     $HOME/bin/monthly
       # at 10 pm on weekdays, annoy Joe with multi line message  (mail reads stdin specified after %)
       0  22 * * 1-5   mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
       23  0-23/2 * * * echo "run 23 minutes after midnight, 2am, 4am (i.e.even hours) ..., everyday"
       5   4 * * sun     echo "run at 5 after 4 every Sunday"

Each user has their own crontab located in /var and is not to be edited directly.
See also /etc/crontab the system wide crontab

Current versions mitigate day-light-savings-time problems.

Check Fcron drop in replacment or cron daemon which runs scripts which should have been run when system was down

Files

/var/spool/cron/user user crontabs
/etc/crontab System crontab file, time/date fields are follolwed by a user name .

If /etc/cron.allow exists, then only listed users have active crontabs
If /etc/cron.allow does not exist and /etc/cron.deny exists, listed users crontabs will not be used.
If neither files exists site configuration can specify that only super user or all users can have active crontabs.

Author Paul Vixie

See Also

For Mac OSX launchctl StartCalendarInterval

fcron cron replacment. includes periodic, considers load, applies nice

anacron On Raspberry Pi BOTH cron and anacron are running,
raspberry pi notes. does not assume that the system is running, min resolution is hour, runs at boot only.

cronless, service that invokes script at yourhost and sends results via SMS or email 198.143.157.208,9,10,11