Quick and Easy Cron Tutorial

Posted by Johan Cyprich on 14 Nov 2007 | Tagged as: Tech Tips

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. (MAILTO=webmaster@website.com).

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

Examples

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.

Related posts:
    Change IP in Linux from the Command Line
    Managing Linux with Webmin
    Bugatti Veyron at 407 Km/h
    Turbo Compilers: They’re Back!

Share this post:

del.icio.us:Quick and Easy Cron Tutorial digg:Quick and Easy Cron Tutorial spurl:Quick and Easy Cron Tutorial wists:Quick and Easy Cron Tutorial simpy:Quick and Easy Cron Tutorial newsvine:Quick and Easy Cron Tutorial blinklist:Quick and Easy Cron Tutorial furl:Quick and Easy Cron Tutorial reddit:Quick and Easy Cron Tutorial fark:Quick and Easy Cron Tutorial blogmarks:Quick and Easy Cron Tutorial Y!:Quick and Easy Cron Tutorial smarking:Quick and Easy Cron Tutorial magnolia:Quick and Easy Cron Tutorial segnalo:Quick and Easy Cron Tutorial gifttagging:Quick and Easy Cron Tutorial

Follow Me:

Did you find this post interesting and useful? You can keep up to date on this blog by subscribing to my RSS feed, or you can have new posts sent to you by e-mail. You can also follow me on Twitter.


Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply