How to setup cron

Posted on October 25 2009 by zemog

I remember few years back when I made a script that should run everyday. I did not know cron that time. What I did was put that script in the home page hoping that in a day one person will open the site so that the script will be triggered. What a lousy idea!

Then I realize that windows has what we called scheduled task so I was thinking that Linux should have that too. That is how I came to know cron.

What is Cron?

Cron is a Linux module that allows you to run commands at predetermined times or intervals. The name Cron is derived from the same word from which we get the word chronology, which means order of time.

Using Cron, a developer can automate tasks as mailing ezines that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Systems administrators and Web hosts might want to generate quota reports on their clients, complete automatic credit card billing, or similar tasks. That is cron!

Syntax for Cron

minuets hours dayofmonth month dayofweek command

All the variables, with the exception of the command itself, are numerical constants. In addition to an asterisk (*), which is a wildcard that allows any value, the ranges permitted for each field are as follows:

  • Minutes: 0-59
  • Hours: 0-23
  • Day_of_month: 1-31
  • Month: 1-12
  • Weekday: 0-6

It is very important that you will follow the right syntax or your cron will execute properly.

So, if we want to run a script every Monday morning at 8:30AM, cron file should contain the following:

30 8 * * 1 /path_to_your_script

Adding the Command to Cron

So now that you know how to use the cron syntax, it is now time to put the command to cron.

Fire up the command on your terminal

# crontab -e

This means you will be editing the crontab

You will then type your command like: 30 8 * * 1 http://www.domainname.com/myscript.php

Execute the crontab

This is very simple. All you need to do is:

# crontab crontab

That will now execute your myscript.php every Monday at 8:30 AM


Share This Post

Comments are closed.