Compare Web Hosting Plans Comparison shopping for a
great web hosting deal!
Home - Budget Hosting - Virtual HostingDedicated ServersReseller Plans - Free Downloads - Articles
 

 

Unix Webserver Crontab Basics



What is crontab?

Crontab is a Unix operating system program that serves as a task scheduler (its one and only purpose). It runs Unix programs at the times and dates that you specify.

Many people seem to get confused whenever they hear the word crontab mentioned. While its usage may seem somewhat difficult, it really isn't once you understand the basics, which is exactly what this short tutorial is about to give you.

Crontab operates by reading a set of Unix instructions you give it which tell it what to do and when to do it. These Unix instructions look similar to this:

0 * * * * cd /home/username/cgi-bin && ./script.cgi

While this may look like ancient Greek to you now, you'll soon see this as it really is, a simple way to tell crontab when to run the script.cgi file in your cgi-bin directory.

The above example is just one Unix command. Your crontab file can have any number of commands in it to run any number of Unix programs on certain days or at certain hours, just by adding one command
per line in the file and forcing crontab to re-read your file.

Let's break down the above example:

0 * * * * cd /home/username/cgi-bin && ./script.cgi

There are two areas in the crontab: The date time area and the actual command. There are five fields in the first area, which determine exactly when the Unix program will run. The syntax for these fields is:

Minute(0-59) Hour(0-23) DayofMonth(1-31) Month(1-12) WeekDay(0-6)

A few things to keep in mind here are that in the weekday field 0 equals Sunday and in the hour field the hours are in 24 hour time format (military time) and that an asterisk [*] can "wildcard" the field, meaning it will match any or all values.

Now let's take a quick look at the example again:

0 * * * * cd /home/username/cgi-bin && ./script.cgi

What the above crontab command says is 'run this script every hour ON the hour".

Now suppose we want something to just run once each day at 12am (midnight). We would use something like this:

0 0 * * * cd /home/username/cgi-bin && ./script.cgi

The above command will run the script at 0 hours and 0 minutes (midnight) every day of every week of every month.

Now you can see how most scripts will operate using the Unix crontab command, but every once in a while you'll need to do something a bit differently, such as have a script run once a week or once a month. Let's see how that works.

Suppose we need a script to run once a week on Sunday and we only want it to run at 1:00pm in the afternoon. We would use the following Unix command:

0 13 * * 0 cd /home/username/cgi-bin && ./script.cgi

Still confused? Okay, let's take a closer look:

0 13 * * 0 is the timing area

0 = zero minutes (on the hour)
13 = 1pm
* * = any monthday & any month
0 = Sunday

Now do you understand how all of this fits together? Using crontab in Unix really is easy when you know the basics. Let's look at one last example:

Suppose you need something to run on the 15th day of every month. We would use this command:

0 0 15 * * cd /home/username/cgi-bin && ./script.cgi

This means that the Unix server would run the script at midnight on the 15th day of each month.
 

About the author:

William Cross is the co-founder of The PHP Labs and spends his time frequenting the Search Engine Optimization forum at WebWorkShop.
 

Home  -    -  Add URL  -  Site Map
© 2003-2006 CompareHosts.com