Difference between revisions of "Cron"
|Line 80:||Line 80:|
Revision as of 18:27, 24 February 2013
Cron is a system service which is used to invoke programs or scripts in a periodic manner. The crontab is the file which contains the schedule of what programs should be called when. Cron itself is a system daemon which runs in the background, wakes up periodically, and runs anything in the schedule whose time has come. crontab is also the command you run to edit the schedule file.
The crontab command can run run with -l option to show the current schedule, and with -e option to launch an editor to modify the schedule. The $VISUAL environment variable determines which editor is launched. For example, set $VISUAL to '/usr/bin/vi' to run vi to edit the schedule.
Each line in the crontab file can be a comment, a variable declaration, or an event line.
Comments begin with a comment mark #, and must be the first character on the line.
Variable declarations are of the form
Unlike bash scripts, you can get away with putting spaces around the = sign.
Each event line specifies a time and a date, and a command which is to be executed, in the format
minute hour date month day command
The first five fields can be numbers or ranges, in the format described below. Note that you can specify either the date (i.e. within the month) or the day (of the week), but not both. The other field should be set to *.
The sixth field is a command with parameters. Percent signs, unless escaped with a \ backslash, will be turned into newlines and everything after the first one of these will be fed into the command's STDIN stream.
It is also possible to execute shell scripts or run various applications with cron. For example. if you want it to start an alarm at 6AM every weekday morning, here is the crontab line you would use:
0 6 * * 1-5 /home/user/alarm.sh
Normally, the crontab file contains a MAILTO variable that directs output (STDOUT and STDERR) to an email to the address (e.g. MAILTO=dave). If this is not working, the script may quit unexpectedly when its output has nowhere to go.
|*/5||Any number, in steps of 5|
|1-6||Any number between 1 and 6 (inclusive)|
|0-30/5||Any number between 0 and 30, in steps of 5|
|1,4,9||1, 4 or 9|
Months can be specified in numbers or in words.
1 = jan
2 = feb
12 = dec
Days of the week
Days of the week also can be specified in numbers or words.
0 = Sunday
1 = Monday
2 = Tuesday
6 - Saturday
7 - Sunday
# fetch e-mail every ten minutes */10 * * * * fetchmail # send myself a birthday greeting 0 9 7 28 * mail -s'Happy Birthday' ajs318%Many Happy Returns - you old fart!%.%% # back up my recipe database every Monday 30 5 * * 1 mysqldump --opt recipes > /home/ajs318/backups/recipes.sql