Quick and Easy Cron Tutorial
Cron is an application in Unix or Linux systems that runs another program or script at a certain date and time. Its runs continuously in the background similar to Scheduled Tasks in Windows. Cron is typically used to run system administration tasks such as e-mailing you the status of your server. In order to use this tutorial, you need open the command shell.
Managing Tasks with crontab
Using crontab is the easiest way to create and edit cron tasks. There are only a few commands you need to know.
|crontab -e||Edits crontab file or creates a new one if it doesn’t exist.|
|crontab -l||List contents of crontab file. Its also useful for looking at crontab after it was edited to make sure everything is ok.|
|crontab -r||Deletes the current crontab file.|
A useful command to place at the top of the crontab file is MAILTO. This will e-mail any error or output from the cron tasks to the recipient. This information should be sent to the webmaster, i.e. (MAILTOfirstname.lastname@example.org).
Cron Command Sequence
Every command in crontab has the following format:
Â Â Â Minute Hour DOM Month DOW Command
|Minute||Minute of the hour, 0-59 (* indicates every minute).|
|Hour||Hour of the day, 0-23 (* indicates every hour).|
|DOM||Day of month, 1-31 (* indicates every day).|
|Month||Month of the year, 1-12 (* indicates every month).|
|DOW||Day of week, 0-6 where 0=Sunday, 1=Monday, 6=Saturday (* indicates every day).|
|Command||The command or script to be executed.|
If you want an event to occur every 30 minutes, use */30 for the Minute. The Command can a PHP script, a program, a Linux command (i.e. echo “hi there”), or a command to run a script on another web server (i.e. lynx -source http://www.website.com/status.php).
|0 19 * * * backup.php||Runs the script, backup.php, every day at 19:00 (or 7:00 PM).|
|*/15 * * * * checkmail.php||Runs checkmail.php every 15 minutes.|
|10 30 * * sun gotochurch.php||Runs gotochurch.php every Sunday at 10:30 AM.|