Unix Webserver Crontab Basics
By William Cross2004-03-26
What is crontab?
Crontab is a unix program that acts as a task scheduler. It serves one purpose and only one. It runs programs at the times or dates that you tell it to. A lot of people seem to get confused whenever the word crontab is mentioned. While the usage may seem somewhat difficult, it really is not once you understand the basics. That is precisely what this small tutorial is about to give you, the basics.
Crontab runs by reading a set of instructions you give it which tell it what to do and when to do it. These instructions look similar to this:
0 * * * * cd /home/username/cgi-bin && ./script.cgi
While this may look like ancient egyptian to you now, you will soon see this as it really is, a way to tell crontab when to run script.cgi in your cgi-bin directory.
The above example is one command. Your crontab file could have any number of commands in it to run any number of programs on certain days or at certain hours, simply by adding one command per line in the file and making crontab re-read your file.
Breaking down the above example.
0 * * * * cd /home/username/cgi-bin && ./script.cgi
Simply put there are 2 areas in the crontab. The date time area and the actual command. There are 5 fields in the first area. These determine exactly when the 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 note here are that in the weekday field 0 equals sunday and in the hour field the hours are in military 24 hour time format and that an asterisk [*] can "wildcard" the field, meaning it will match all or any values.
Now let's look at our example again:
0 * * * * cd /home/username/cgi-bin && ./script.cgi
What the above says is that this will run this script every hour ON the hour.
Now say we want something to just run once a day at midnight. We would use something like this:
0 0 * * * cd /home/username/cgi-bin && ./script.cgi
The above will run at 0 hours and 0 minutes every day of every week of every month.
Here you can see how most scripts operate using crontab. But once in a while you need to really do something differently and have a script run once a week or once a month. Let's see how that works.
Okay, say we need a script to run once a week on sunday and we only want it to run at 1pm in the afternoon. We would use the following commandline:
0 13 * * 0 cd /home/username/cgi-bin && ./script.cgi
Confused still? Okay, let's look at it closer.
0 13 * * 0 is the timing area
0 = zero minutes (on the hour),
13 = 1pm,
* * = any monthday & any month,
0 = sunday
Now do you see how this all fits together? It really is easy when you know the basics. One last example before we go.
Say you want something to run on the 15th day of every month. We would use the following:
0 0 15 * * cd /home/username/cgi-bin && ./script.cgi
This means that the server would run the script at midnight on the 15th day of the month. Can you see why? If not then go back to the top of this tutorial and read it again until you do.
Tutorial Pages:
» What is crontab?
