Toolserver:Cron

cron is a Unix daemon that allows users to schedule programs to run at set intervals. A crontab is the file that cron uses to determine what actions to perform and when. crontab is also the command used to edit the crontab file. A cronjob is a the specific line (or job) in the crontab. For a description of how to use cron, read the crontab(1) manual page. A simplified description of cron is given below.

For one-shot cron jobs (those that perform a task then exit), we strongly recommend using batch job scheduling. That page explains how to use cron with batch jobs; you should still read this page to understand how cron works. (Note: you should not use batch jobs for phoenix cron jobs, or any other continuous tasks.)

Basic commands
crontab has three basic commands:


 * $ crontab -e : edits (or creates) your current crontab in a text editor
 * $ crontab -l : displays your crontab
 * $ crontab -r : removes your crontab entirely

You can change the text editor used for editing the crontab in your environment.

crontab syntax
Each line in the crontab file is a single cron job. The line contains five fields at the start indicating when the job should run, and the rest of the line is the command.

minute (0-59), |      hour (0-23), |      |       day of the month (1-31), |      |       |       month of the year (1-12), |      |       |       |       day of the week (0-6 with 0=Sunday). |      |       |       |       |       commands 30     2       *       *       0,6     /some/command/to/run 30     2       *       *       1-5     /another/command/to/run >/dev/null

Use "*" to mean "every"; for example, "30 2 * * *" runs the program at 02:30 UTC, every day of every month. Only specify "day of month" or "day of week", not both.

On Linux, you can write "*/x" to mean "every x". For example, writing "*/10" in the "minute" column means "every ten minutes". This doesn't work on Solaris; instead, you can write "10,20,30,40,50" (this format can be used on linux as well).

The crontab file must end with a blank line (that is, the last character of the final line in the file must be a newline character). Many editors, like nano (the default editor), end all files with a blank line by default. Note that the editor "joe" is not one such editor - be sure to end your file with a blank line every time. If you don't, you won't receive an error message, but nothing will happen at the scheduled time.

An example which would run every hour on the hour would be: 0 * * * * php $HOME/scripts/myscript.php

This means every hour, i.e. *, at minute 0. There is no need to write */1 for the hour field, as */1 is identical to *.

Lines beginning with a # will be treated as comments, and ignored, along with blank lines.

Using batch jobs
To run cron jobs as batch jobs, use a command line this: 0 * * * * cronsub myjob % php $HOME/scripts/myscript.php

(On Linux, use "/usr/local/bin/cronsub" instead.)

This will automatically run your job on a less busy server, so it will run quicker. For more information, see Batch job scheduling.

Linux shortcuts
Note: these are non-portable and only work on Linux.

string         meaning --         --- @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 * * * *".

Output
Cron will email you every time a command produces output. To avoid this, you can redirect the output to a file, or discard it.

Adding  on the command redirects normal output to nowhere. This will still email you error messages; you can avoid that by appending, which sends fd 2 (stderr, i.e. error messages) to the same place as 1 (stdout, i.e. normal output). If you want those to go elsewhere, then specify that instead of, for example:

0 * * * * php $HOME/scripts/myscript.php >/dev/null
 * 1) Redirect normal output to nowhere

You can also change the address to which cron will send output by writing a MAILTO line in your crontab: MAILTO=foo@example.com

Alternatively, put an email in. Multi-maintainer projects may wish to put one email per line for all members (using your  email is allowed, it will be forwarded according to the   file in the user's own account).