Webmaster Workshop
The Beginner's Guides Section

Front Page  Forums    Contact Us   Terms of Use   How To Guides  Resource Center


Hyperseek Search Engine
Ready to play with the big boys?


GUIDES

GENERAL

Choosing a Host

Hosting Terminology

Protection of contents


SCRIPTING

Beginners Guide to Telnet

Basic UNIX Commands

CHMOD and setting
permissions Guide

Basic guide to CRONTAB

Basic Guide to Paths

CRONTAB GUIDE

What is crontab ( also referred to as cron and cronjob )?

It is a method of automating  tasks that are preformed on a regular basis.

The main use of this for a webmaster is to run a script at regular intervals which perform a task like refreshing content, creating backups, mailing users during the server's quiet time or some such similar task.

Firstly ask your host to setup or grant you permission to use crontab if not supplied with your hosting package.

A crontab entry consists of six fields the first five set the frequency of the execution of the sixth field which contains the command to be carried out .

The first five fields are as follows 

  1. The number of minutes after the hour ( 0 to 59 )

  2. The hour in 24 hour format  ( 0 to 23 )

  3. The day of the month ( 1 to 31 )

  4. The month  ( 1 to 12 )

  5. The day of the week { 0 (Sunday) to 6 (Saturday) }

There are also three operators that can be included in the first five fields

  • Astericks * are used to mean every instance in the field
    e.g. in the field of the day it would mean this command 
    will run everyday

  • Commas , are used to separate values in a field
    e.g.
    in the field of hours 1,4,7 would mean this command 
    should run at 1o'clock 4o'clock and 7 o'clock 

  • Dashes - are used to include every value between the first and last value
    e.g.
    in the day of the week field 1-5 would mean run on
    Monday to Friday inclusive.

  • Note there should be no spaces between the value comma or dash and the last value
    e.g.  1,5,7 is okay    1 , 5 ,7  is incorrect   2-4 is okay    2 - 4 is incorrect

  • Each field is separated by a space to denote the start of a new field
    this is why you must not leave a space in the values of a field (see above)

Let's get those first five fields understood before moving to the sixth field by trying some examples

20 5 * * *    This would mean that the command should be run at 20 minutes past
                         5  Every day. 

* 4 * * *      This means the command would run at 4am everyday

* 2,10,22 * * *  This would run the command at 2am, 10am and 10pm Everyday

20 5 * 1-8 * This means your command would run at 5 20am daily from
                          January to August

3 0 * * 4  Here your command would run at 3 mins past midnight on a Wednesday


The sixth field containing the command

There are many commands that may be used in this field but here I will only outline the running of a cgi script which is the most common usage by the webmaster.

Firstly if we wished to run a script called bulkmail.cgi we would enter the full path to the script.

NOTE this must start with a  /

so this field could look something like   /usr/local/apache/www/cgi-bin/bulkmail.cgi

or to run listit.pl it would be  /usr/local/apache/www/cgi-bin/listit.pl

if this field had some output to write a webpage for you for example and it was writing it to output.html in your news directory (http://yourdomain.com/news/output.html )

your command would look something like this

/usr/local/apache/www/cgi-bin/listit.pl>/usr/local/apache/www/htdocs/news/output.html

Note the > operator meaning output to..

Now if you understand all the above about the 6 fields and what they do we can move onto the actual crontab command


Firstly let us create a crontab file to run our bulkmail script called bulkmail.cgi at 4 am every Wednesday morning .The reason for doing this may be that you wish to preform this mailing task at a quiet time for the server.
Also we wish to update our news page with the latest headlines at 6am,1pm and 9pm everyday by running our script called listit.pl and outputting the news to output.html in our news directory.

The first step is to open your text editor.

Firstly switch OFF word wrap if it has this feature.

Next type in the commands which could look like this using the info above

* 4 * * 3 /usr/local/apache/www/cgi-bin/bulkmail.cgi
* 6,13,21 * *
* /usr/local/apache/www/cgi-bin/listit.pl>/usr/local/apache/www/htdocs/news/output.html

Now press return again so you have a blank line at the bottom.
 THIS IS VERY IMPORTANT

Now save the file as a text file something like mycron.txt and then you would upload it to your server placing it in your top level directory (the directory you are in when you log on to your server.)

Now telnet into your server and at the command prompt type

crontab -l       (note lowercase L ) (this command will list the crontab file)

if the cron has been setup for you you should get a file open up.

If this happens you are ready to load in your file to do this type at the command prompt

crontab mycron.txt 

And by the magic of the computer age your file will be transferred into the cron file.

Check this out by typing crontab -l again

There are two other switches for crontab command they are 

crontab -e     ( this opens the file in the server default editor normally VI )

crontab -r      (this will remove (delete) the crontab file )

So create your file containing the list of commands save as a text file upload it to your server. Telnet in and type crontab filename.txt to update your cron job

DO NOT FORGET THAT BLANK LINE AT THE END OF THE FILE !!


This site design and content is Copyright 2000-2005 by
Webmaster Workshop.com
Also the content supplied by other organizations is subject to their
respective Trade Marks and Copyright Policies

Webmaster