cron jobs are automated scripts that run at a specified date/time, or on a specified interval.
anything that can be executed in the terminal on the server can be run as a cron job (this includes commands, applications, scripts, etc).
crontab is the command used to create/view/modify/remove cron jobs.
- crontab -l lists all scheduled jobs
- crontab -e allows you to edit jobs
- crontab -r will delete *all* your active jobs!
once the crontab file has been edited the cron daemon will automatically read it and update it's job que to match the file. you should see the message:
crontab: installing new crontab
crontab file syntax
the file can be separated into two parts:
- the interval to run the job
- the command to be executed
here's a cheatsheet for the interval syntax
.---------------- Minute (0 - 59) | .------------- Hour (0 - 23) | | .---------- Day of the Month (1 - 31) | | | .------- Month (1 - 12) | | | | .---- Day of the Week (0 - 6) (Sunday=0 or 7) | | | | | * * * * * example-script.sh
here's an example entry:
this job would run every half an hour on monday through friday.
30 * * * 1-5 echo "cron job executed"
valid time values
Minute 0-59 (* every minute at the start of each minute) Hour 0-23 (* every hour at the start of every hour) Day of Month 0-31 (* every day) Month 1-12 (* every month, you can use month names if you prefer) Day of Week 0-7 (0 and 7 both mean sunday, again you can use names - see below)
an alternative to the six field crontab syntax would be the two field option which consists of two fields the date / time and the actual command.
here's a list of the most common @ options for cron:
@reboot - runs when the machine is started up or if the cron daemon is restarted @midnight - runs once a day at midnight (the equivalent of 0 0 * * *) @daily - alias of @midnight @weekly - runs once a week on sunday (the equivalent of 0 0 * * 0) @monthly - runs once a month on the first day of every month at midnight (the equivalent of 0 0 1 * *) @annually - runs once a year at midnight on the first day of the first month and (the equivalent of 0 0 1 1 *) @yearly - alias of @annually
on the very first line of the crontab file you can specify an email address to send the results of the job to. using the following syntax: MAILTOemail@example.com
the raw output of your job is will be emailed to the supplied address. the results could be echo statements, html, xml, json, whatever.
on march 12th at 7:45pm load the http://x-e.ro/cron/job page
45 19 12 3 * curl --silent --compressed http://x-e.ro/cron/job
chaining multiple commands is exactly like regular shell commands. link them with &&, ||, or ;
@midnight cd /var/www/x-e.ro/ && php cron.php
every night at midnight load the http://x-e.ro/cron/job/2 with the supplied username/password
@daily curl -u username:password --silent --compressed http://x-e.ro/cron/job/2
a note about passwords in cron jobs
if your password contains a special character (it should!) that might interfere with your bash script's execution, simply escape the character with a prepending backslash. so if your password was `P4s$w0rd!` use:
@daily curl -u xero:P4s$w0rd\! --silent --compressed http://x-e.ro/cron/job/3
an important note about cronjobs is the fact that the execution time is tied directly to the server's time. make sure the server's time is in the timezone that you expect before setting up your jobs. execute the `date` command on the server and look at the time code on the end of the results.
Thu Apr 18 11:52:19 EDT 2013
the EDT suffix indicates the server is running on Eastern Daylight Time so it would execute in our local east coast time taking daylight savings into account.
Thu Apr 18 11:52:19 UTC 2013
the UTC suffix indicates the server is running on Coordinated Universal Time so the server's time is four hours *ahead* of our local east coast time.