Wednesday, October 8, 2008

How to Create a Cron Job (Scheduled Task) for Your Website or Blog

by Christopher Heng, thesitewizard.com

On occasion, you might come across the need to create a scheduled task for your site. For example, you may have installed a website statistics software such as Awstats or a content management system such as Drupal that requires a background program to run at a certain time. In such a case, the software's documentation often asks you to schedule a cron job on your web server. This article provides a step-by-step tutorial on how you can schedule such a task using a program named crontab.

System Requirements

  1. An Appropriate Operating System

    Cron jobs, created using the command line program called crontab, require that your website be hosted on a Unix-type web server, such as Linux or one of the BSDs. Generally, if the web server that your site is on is running Windows, you cannot use this tutorial. Note that I am referring to the computer which your web host uses to place your site, not your own computer.

  2. Shell Access or Control Panels Interface to Crontab

    You also need to be able to connect to your web host using telnet or SSH. Alternatively, your web host must provide you a way to set the crontab tasks using their control panel.

Overview

Basically, we will start by creating a schedule. Then, we will feed this schedule to a program called crontab. Crontab will take the schedule and install it into an internal set of tables which it manages. At the appropriate time demanded by your schedule, another program called cron will execute the tasks you have set.

Steps to Setting Up a Cronjob on Your Web Server

  1. Figure out the schedule

    The first thing you need to do is to figure out a schedule for your task. Do you want it run once a day? Hourly? Note that if you use a shared web server, where many websites reside on the same web servers, you should not run your cronjob too frequently, or you will affect the performance of both your website and the other sites hosted on the same computer (and get you booted off the web host as well). If you really need such a high frequency, you may need to get a virtual private server or even a dedicated server where you have the machine to yourself.

  2. Figure out how to write the schedule for crontab

    The next thing you need to do is to write your schedule in a way so that crontab will understand it. The crontab format is somewhat arcane and cryptic, so we will take this step by step.

    The basic format of a crontab schedule consists of 6 fields, separated by spaces, formatted as follows:

    minute hour day month day-of-week command-line-to-execute

    The acceptable values for each of the 6 fields are:

    FieldRange of values
    minute0-59
    hour0-23
    day1-31
    month1-12
    day-of-week0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)
    command-line-to-executethe command to run along with the parameters to that command if any

    The fields have to be in that exact order, with no empty or missing fields

  3. Now if you want a job to run every hour on the hour, you will have to set the time component of the crontab line as follows:

    0 * * * *

    Can you see why? The "0" means at the top of the hour, that is, when the minute readout on a digital clock shows "00". The asterisk in the hour field means every single hour. In other words, every hour, on the hour.

    Alternate Hour or 3 Hourly Schedule

    If you want something to run once every two hours, you will have to use the slash, "/", character in your field. The slash character is the "step" character. In the case of a two hourly schedule, your time component of your cron file will read:

    * */2 * * * *

    The second field, "*/2", means every alternate hour.

    Similarly, if you want something to run every 3 hours, you can change that field to "*/3", and so on.

    Other Examples

    If you want a particular command to run only at 8.00am on the 1st and 20th of every month, you should code the time as:

    0 8 1,20 * * *

    The comma, ",", means "and". If you are confused by the above line, remember that spaces are the field separators, not commas.

    What does the following schedule mean?

    2 3 4,5 6 7

    Decoded, the above line says at 3:02 am on the 4th and 5th of June (6) and on every Sunday (7), run your program.

    There are other possibilities in the time fields, and I won't go through all of them, since you already know enough to be able to construct whatever schedule you need.

You may get more details at:
How to Create a Cron Job (Scheduled Task) for Your Website or Blog

No comments:

 
Custom Search