crontab 

Send to Kindle
home » snippets » crontab



Notes

Environment Variables

Several environment variables are set up automatically by the cron daemon.  You can override all except LOGNAME by writing lines of the form VAR=VALUE.

NOTE:  The RHS is not expanded by the shell so you can't refer to other variables or expect other forms of expansion to work.

Variable Description
SHELL Set to /bin/sh by cron
HOME Set from /etc/passwd line of the crontab user.
LOGNAME Set from /etc/passwd line of the crontab user.  Cannot be overridden in the crontab file.
PATH Initially set to some system default value.  If you want your own custom PATH, you must set it explicitly yourself.
CRON_TZ specifies the time zone specific for the cron table. The user should enter a time according to the specified time zone into the table. The time used for writing into a log file is taken from the local time zone, where the daemon is running.
RANDOM_DELAY allows delaying job startups by random amount of minutes with upper limit specified by the variable.  The random scaling factor is determined during the cron daemon startup so it remains constant for the whole run time of the daemon.
MAILTO CSV of email addresses to send the output of cron jobs.  Used if a mail needs to be sent.  If MAILTO is defined (and non-empty), mail is sent to the specified address. If MAILTO is defined but empty (MAILTO=""), no mail is sent.  Otherwise, mail is sent to the owner of the crontab.  NOTE: It applies to all cron entries after the MAILTO declaration.  You can define it multiple times in the file.
MAILFROM If MAILFROM is defined (and non-empty), it is used as the envelope sender address when sending mail, otherwise, root is used.
CONTENT_TYPE The content-type to use for cron output emails.
CONTENT_TRANSFER_ENCODING The charset to use for cron output emails.

Specifying the command

Note:  All % characters in the command part of a crontab line are changed to \n (newline) characters unless escaped by a backslash character (\) and all data after the first % will be sent to the command as standard input.

Special / shorthand time extensions

You can replace the first 5 fields that specify the time with one of these shorthands.

Shorthand Description
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".

Specifying the times

The time and date fields are:

Field Allowed Values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sunday, or use names)

A field may contain an asterisk (*), which always stands for "first-last".

Ranges of numbers are allowed.  Ranges are two numbers separated with a hyphen.  The specified range is inclusive.

Lists are allowed.  A list is a set of numbers (or ranges) separated by commas.  Examples: "1,2,5,9", "0-4,8-12".

Step values can be used in conjunction with ranges.  Following a range with "/<number>" specifies skips of the number's value through the range.  For example, "0-23/2" can be used in the 'hours' field to specify command execution for every other hour (the alternative in the V7 standard is "0,2,4,6,8,10,12,14,16,18,20,22").  Step values are also permitted after an asterisk, so if specifying a job to be run every two hours, you can use "*/2".

Names can also be used for the 'month' and 'day of week' fields.  Use the first three letters of the particular day or month (case does not matter).  Ranges or lists of names are not allowed.

The "sixth" field (the rest of the line) specifies the command to be run.  The entire command portion of the line, up to a newline or a "%" character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.  % characters are replaced by newlines as described in the Specifying the command section.

Note:  The day of a command's execution can be specified in the following two fields — 'day of month', and 'day of week'.  If both fields are restricted (i.e., do not contain the "*" character), the command will be run when either field matches the current time.  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.

Snippets

# .---------------- minute (0 - 59)
# |   .------------- hour (0 - 23)
# |   |   .---------- day of month (1 - 31)
# |   |   |   .------- month (1 - 12) OR jan,feb,mar,apr ...
# |   |   |   |  .----- day of week (0 - 7) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |   |   |   |  |
# *   *   *   *  *  command to be executed