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.

On the Toolserver, we strongly recommend that cron be used in conjunction with batch job scheduling. This page explains how to do this.

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. Use a comma to run at multiple times; for example, to run once every 10 minutes, you could use "0,10,20,30,40,50" in the "minute" field.

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) and vi do this by default. The "joe" editor does not do this - 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.

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

Using cron with batch job scheduling
We recommend that all tools be run as batch jobs, which has several advantages over standard jobs (for example, jobs will be automatically started on a suitably idle host instead of always running on the same host). To run jobs this way, start them with the cronsub command: 0 * * * * cronsub -s myscript % php $HOME/scripts/myscript.php

The second argument, "myscript", is the name of the job. This should be different for each job. The % should separate the cronsub command from the actual command to be run.

Note that jobs run this way will behave slightly differently, for example job output will be written to a file, $HOME/myscript.out, instead of being mailed to you. For more information, see Batch job scheduling.

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).