Cron, crontab example reference and how to run things periodically in GNU/Linux and Unix

Cron is a job scheduler found in most Unix-like operating systems. “Chronos”, which is the Greek word for “time”, is where the name cron comes from. Cron makes it possible to schedule jobs, which can be commands, a series of commands, or scripts, that you want to run periodically. Common uses are backups, notifications, periodic checks on availability of services, networks, machines, or other things, and administration and maintenance tasks like rotating of logs, to mention a few. It is very general-purpose though, (like Unix tools is and should be) and can be used for whatever you can think of that needs to run periodically.
Image showing the Big Ben clock.

Jobs that should be run by cron are specified in crontab (cron table) files. A crontab is a configuration file that specifies commands (shell commands, including programs, scripts, or pipelines thereof) to be run, and when they should be executed.

Each line in a crontab file represents a “job”, and is composed of a CRON expression (specifying the time) followed by the command or commands to be executed. There are two different crontab files, the ones installed by system software, and editable only by root (but in most cases best left alone), and crontab files written by and belonging to users (including root). The authorization control to cron, who will be allowed to have a crontab or not, are done with the files /etc/cron.allow and /etc/cron.deny.

The manpages of cron and the crontab format are nice. “crontab” is both the name of the program installing, listing and editing user’s crontabs, and the file format manpage, so do read them:

man cron
man 1 crontab
man 5 crontab

The page in section 5, about the format is most interesting. However I prefer to keep a short reference in my user’s crontab file itself for quick reference, feel free to grab it:

# mycrontab.txt
# beardy
# My example crontab, kept in ~/

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Send the mail to another address than the local user
##MAILTO=foo@foo.bar

##############################################################################
#
# The following example should run "true" every day of every month, at half
# past eight, every day of the week.
#
# Example:
#
# 30 8  * * *   true
# |  |  | | |    |
# |  |  | | |    +--- command, or script, to run
# |  |  | | +-------- day of week, 0-7 or mon, Tue, FRI
# |  |  | +---------- month, 1-12, or names, jan, Mar, AUG (case doesn't matter)
# |  |  +------------ date, 1-31
# |  +--------------- hour, 0-23
# +------------------ minute, 0-59
#
# see "man 5 crontab"
#
#     Instead of the first five fields, one of eight special strings may
#     appear:
#
#           string          meaning
#           ------          -------
#           @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 * * * *".
#
##############################################################################

# Run beep every Christmas Eve, at 14:40 (20 min before Kalle Anka)
#40 14  24 12 * beep -f 1000 -r 5 -l 150 -D 250
# next job goes after this line
40 14   24 12 * echo "God Jul!"

To install a new crontab, the crontab(1) program is used:

crontab mycrontab.txt

To later list it, and edit it (with the editor configured with the $EDITOR environment variable usually):

crontab -l
crontab -e

Read more about cron and crontab:

cron(8)
crontab(1)
crontab(5)
Time – An important Subject with any OS