web hints
Web Design   Domains   Hosting   SEO   Traffic Generation   PPC/Advertising   Business Tips   Partners

Unix Webserver Crontab Basics

by William Cross

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.

William Cross is the co-founder of The PHP Labs located at http://www.phplabs.com and spends his time frequenting the Search Engine Optimization forum at WebWorkShop located at http://www.webworkshop.net/seoforum

More Site Building articles:

12 Characteristics of a Successful Web Site
80% of Your Web Site is Maintenance
9 Near Fatal Flaws Most Links Pages Suffer From and How to Avoid Them
A Quick Web Credibility Quiz
A Quick Website Makeover Quiz
Adding records to a MySQL database using PHP
Blogs and Journalism
Create Cool Site Navigation in a Flash
Download Speed
Everybody's Talking About RSS
Free Autoresponders
How Much Should You Protect?
How Multiple Server Hosting impacts your website's uptime
How to Create and Send an HTML Email Form Using PHP
How to Get Your Visitors to Create Content for Your Website
How to Install a CGI Script and Increase Web Site Interactivity
How to Make Effective Use of Web Fonts
How To Stop Your Site Disappearing Into The Void
Images on web sites: when should they be used?
Is Your Website's Copy Up to the Mark?
Making it Tougher for Content Thieves
Six Ways to Save Your Site and the Internet
Step-by-step Guide To Building A Successful Website (Part 1)
The Most Important Question To Ask About Your Web Site!
The Pop Up Evolution. Are pop ups doomed? Or just vitally challenged?
Top 5 Must-Haves on Every Web
Unicode bloopers! HTML should not be visible on a web page!
Unix Webserver Crontab Basics
Update Your Site Instantly Using SSI
Using Tables to Format Your Web Page
Web Servers: Serving Up the Future
Websites for Search Engines or People?
What to avoid to make your website design effective?

Web Hints by Rolamtech

UK Hotels Guide  Finance Website Links  Web Hosting Guide

a b c d e f g h i j k l m n o p q r s t u v w x y z 1 2 3 4 5 6 7 8 9