We're busy adding support for Internet Explorer 6, but if you've been considering an upgrade you can upgrade Internet Explorer, or download Firefox for free.

Our site is best viewed on standards-compliant browsers: Firefox, Safari, Internet Explorer 7.0+, etc.

Travis

Anatomy of a Cron Job

Posted on 07/03/2008 by Travis
1 Comment

For a long time, cron jobs have been a mystery to me. They seemed much too complicated and I didn't understand them. Whenever I needed to create one with our old host, they had a handy gui-based cron job creator. Still not the most intuitive thing I've ever used, but it got the job done. Now that we've switched over to Rails Machine for our hosting, there is no concept of a gui cron wizard for me to rely on anymore. It was time I put on my big-boy pants and learned to write a cron job like the adults do.

Like most things that intimidate me, I found that there wasn't much to it when I actually dove in and starting researching the topic. Now, I consider myself to be, at the least, competent at creating new cron jobs. Below is a description of the formatting of cron jobs as well as a few examples. I'm keeping this post mostly as a reference for myself, but if it can help anyone else then that's great.

Explanation of how a cron job is formatted

 *   *   *   *   *  some command or group of commands
(1)(2)(3)(4)(5)

(1) minute (00-59)
(2) hour (in 24-hour format of course, 0-23)
(3) day (1-31)
(4) month (1-12)
(5) day of week (0-7 where Sunday is 0 AND 7)

Examples

We want to clear the log files at 2am every Sunday morning.

00  2  *  *  0  cd /var/www/apps/plexus/current; rake log:clear

We want to run some rake task every weekday at noon.

00  12  *  *  1-5  cd /var/www/apps/plexus/current; rake some_task

Let's say we only want to run a script on the 1st and 15th of the month at midnight, and only in the month of June

00  0  1,15  6  *  /path/to/script

If you specify both a month day AND a weekday, then the cron will treat it as cumulative and run on both days. The following script will be executed every Saturday and Sunday at 12:00pm, but will also be run on the 10th of each month.

00  12  10  *  0,6  /path/to/script

Not so bad, is it?

Tagged:  cron job, cron

Comments

Posted by Colin on August 21, 2008

Great summary!

Post a Comment

Name

Comment

simple_captcha.jpg
Please type the letters from the image above.