inicio mail me! sindicaci;ón

Teaching Cron to Run Every Ten Minutes

I got tired of hacking cron and typing in ridiculous things like 0,5,10,15,20,25,30,35,40,45,50,55 to tell my cron jobs to run every five minutes.

Knowing that if I was annoyed by it, someone else with more time had probably suffered too, I hit Google. The result was surprisingly easy…

Cron jobs are run by using the syntax:

crontab -e

Or, to edit a user’s crontab as root:

crontab -u username -e

The format of the cron file is as follows:

#Minute (0-59)  Hour (0-23)  Day of Month (1-31)  Month (1-12 or Jan-Dec)  Day of Week (0-6 or Sun-Sat)  Command

A * equals “all” and is equivalent to using something like 0-59 for minutes. You can also use commas to specify multiples. Examples:

#Minute (0-59)  Hour (0-23)  Day of Month (1-31)  Month (1-12 or Jan-Dec)  Day of Week (0-6 or Sun-Sat)  Command
 
#runs updatedb command on Sunday and Wednesday at 2:15 am
15 2 * * Sun,Wed updatedb 
 
#runs a php script every minute and discards the output (errors too)
* * * * * /home/jsmith/myscript.php >> /dev/null 2>&1
 
#runs php script every 15 minutes and outputs results to /var/log/myscript
15,30,45 * * * * * /home/jsmith/myscript.php >> /var/log/myscript 2>&1

I didn’t want to run my script every minute, nor did I want to type in 0,10,20,30,40,50 to make it run. So I went googling and found this syntax:

#runs php script every 10 minutes
*/10 * * * * /home/jsmith/myscript.php

The */x notation is useful on any field. For instance, */2 in the hours field means every 2 hours, */4 in the months field means every quarter.

Yay!

Comments are closed.