Updated: 2012-12-03 03:56 EST

1 The  cron  daemonIndexup to index

Unix/Linux has a program named cron that runs other programs at scheduled times, e.g. run a backup program every day at midnight.

The cron daemon examines and runs crontab entries once every minute. (The cron daemon can’t run anything more often than once a minute.)

The cron daemon is usually started from a Run Level script, and it may not be configured to run in all Run Levels. It produces log files that are usually kept under /var/log; the actual location will be determined by your syslog configuration settings.

The cron runs both system programs, reading the /etc/crontab file, and personal programs, created by the crontab program.

2 The  /etc/crontab  fileIndexup to index

There is a system cron configuration file /etc/crontab that can run scheduled commands as any userid (usually as root). You can (as root) use a text editor to edit the system /etc/crontab file. Changes take effect as soon as the file is saved.

Unlike personal crontab files, the system crontab command lines are prefixed by a userid field that tells which user to use to run the commands. (The usual user is root.)

3 Personal  crontab  filesIndexup to index

Most systems also allow individual users to have private crontab files for commands they want to run as themselves. Users must use the crontab command to manage their personal cron files. Changes take effect when the crontab command saves your personal crontab file and exits.

To edit your personal crontab file, the crontab command uses the text editor specified in the VISUAL or EDITOR environment variables.

Unlike the system crontab file /etc/crontab, lines in personal crontab files do not start with a leading userid fied.

4 Running as another userid in the system crontabIndexup to index

The system crontab file /etc/crontab needs a userid field at the start of each command line to know as which user to run the cron job. The personal crontab file does not contain a leading userid field, since you can only run jobs as your own userid:

# example line from the system crontab file /etc/crontab (root userid):
17 * * * * root cd / && run-parts --report /etc/cron.hourly

# example line from a personal (non-root) crontab file (no userid):
34 12 * * * echo "It's 12:34 and time to eat lunch" | write alleni

The userid field comes after the time specification and before the text of the command line to run.

5 The crontab time/date file formatIndexup to index

The five time and date fields at the start of crontab lines are (from man 5 crontab):

crontab field allowed values
------------- --------------
minute        0-59
hour          0-23
day of month  1-31
month         1-12 (or names, if using a recent version of cron)
day of week   0-7 (0 or 7 is Sun, or use names)

Any field may be a list, separated by commas (no blanks allowed), or an asterisk (“*“) indicating”every" or “all”.

The minimum resolution of the crontab is one minute, with some systems only running the crontab every 15 minutes.

6 Personal  crontab  ExamplesIndexup to index

Below are some examples of personal crontab entries; there is no userid field in the lines:

Write a message to the user “alleni”:

*  *  *  *  *  echo "1. one" | write alleni
30 *  *  *  *  echo "2. two" | write alleni
30 14 *  *  *  echo "3. three" | write alleni
30 14 15 *  *  echo "4. four" | write alleni
30 14 15 10 *  echo "5. five" | write alleni
30 14 *  *  6  echo "6. six" | write alleni

Key to each of the above lines:

1. every minute of every day of every month
2. 30 minutes after every hour of every day of every month
3. at 14:30 every day of every month
4. at 14:30 on day 15 of every month
5. at 14:30 on day 15 of month 10 (October)
6. at 14:30 every Saturday

Below is an exampel that sends the current Ottawa temperature to a cell phone every hour. This requires the “elinks” package and some form of “mail” or “mailx” package to be installed and configured to send Internet email. (To find your phone gateway, try http://www.notepage.net/smtp.htm.)

01 * * * * elinks -dump -no-numbering -no-references 'http://text.www.weatheroffice.gc.ca/forecast/city_e.html?on-118' | grep -A1 'Temp' | mail -s "Temperature" 6135551212@msg.example.com

See man 5 crontab for both personal and system crontab file examples.

7 The  at  command - run onceIndexup to index

If you want to schedule some commands to happen just once at some time in the future, use the at command followed by a time and day. Enter the commands you want to run, then signal EOF:

$ at 12:00 wednesday
at> echo "lunch with Richard and Linus" | mail -s "Lunch Date" alleni
at> ^D
job 1 at Wed Dec  5 12:00:00 2012
$ 

The command accepts a wide range of time and date formats. (See the man page for at.) The standard error and standard output from your commands, if any, will be sent to you by email when the job finishes.

You can use the atq command to display the list (“queue”) of pending jobs to be run. You can cancel (“remove”) a scheduled job using atrm:

$ atq
1   Wed Dec  5 12:00:00 2012 a idallen
$ atrm 1
$ atq
$
Author: 
| Ian! D. Allen  -  idallen@idallen.ca  -  Ottawa, Ontario, Canada
| Home Page: http://idallen.com/   Contact Improv: http://contactimprov.ca/
| College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/
| Defend digital freedom:  http://eff.org/  and have fun:  http://fools.ca/

Plain Text - plain text version of this page in Pandoc Markdown format

Campaign for non-browser-specific HTML   Valid XHTML 1.0 Transitional   Valid CSS!   Creative Commons by nc sa 3.0   Hacker Ideals Emblem   Author Ian! D. Allen