The Wayback Machine - https://web.archive.org/all/20051104025245/http://www.interspire.com:80/whitepapers/index.php?pageName=An_Introduction_to_Cron

cart   client area   sitemap   forum   contact us

Search Interspire.com for:

Other Resources

   Free Templates
   Case Studies
   White Papers
   Staff Blogs
   Recommended Hosts


An Introduction to Cron
By Joseph De Araujo, Interspire.com        Click here to load the printer friendly version View Print Version  « Back to White Papers List

Introduction

Cron is a schedule based service that runs on just about every UNIX server. It allows you to run commands and scripts at a certain time and date that you can set in a “crontab”. Cron can be set to run any Perl or PHP script and basically any command that can be run from a command line.

In this tutorial I will explain exactly what cron is and how you can use Cron to schedule the execution of scripts on your web server.

I will also show you briefly how to create a crontab in Plesk Reloaded and in Cpanel (Xskin) using the online control panels. I will be using SendStudio as the application that I am setting up the cron job for. The actual SendStudio script will be used to send out emails and autoresponders.



Getting Started
In order to get started with cron, you must have a server that is currently running cron. Your host will also be able to tell you where you are able to setup crontabs -- whether it’s from a web based control panel, or through a telnet terminal window.

A typical crontab and color-coded crontab reference
Let’s explore and begin to understand the crontab command and then we’ll be able to apply it using the method that we have available (via control panel or telnet).
Look at the following basic crontab:



I’ll color code each ‘section’ to allow me to visually explain the different parts of this crontab.



Here is an explanation of the above cron command:
Minute of the hour
Set to a number between 0 – 59 for the corresponding minute of the hour or * for every minute
Hour of the day
Set to a number between 0-23 for the corresponding hour of the day or * for every hour
Day of the month
Set to number between 1-31 for the corresponding day of the month or * for every day
Month of the year
Set to a number between 1-12 for the corresponding month of the year or * for every month You can use the name of the month of the year as well eg. march (not case sensitive)
Day of the week
Set to a number between 0-7 for the corresponding day of the week (‘0’ or ‘7’ is Sunday) You can use the name of the day of the week as well. Eg. Tuesday (not case sensitive)
Beginning of the command line
Here you specify the path to PHP or Perl and any ‘flags’ you might need.
The path to your script
Specify the path to your script that you want to run.

What is the asterisk and what does it do?
The Asterisk specifies that the script should be run on every occurance of a time change. For example, if you want to set a cron to run every hour, then you would place an asterisk in the ‘hour’ column. Eg. 0 * 1 1 1 Setting a range within a column
You can set a range of values in the cron command so that it will run within that range. Eg. 2-5 will run 2, 3, 4 and 5 of the column you set it in. In the day of the month column, for example, this 2-5 setting will run on the 2nd, 3rd, 4th and 5th of the month and will look like this. 0 * 2-5 * * Setting the frequency within each column
If you wanted to run a cronjob every 5 minutes, you would be able to use */5 in the minutes column. If it was every 5 hours, then */5 could be placed in the hours column. Many would confuse this */5 as a fraction, but it is actually setting the frequency that the cron takes place in that column. It means ‘every five’ rather than a fifth of it. The command line
This section can be any command that can be run from a command line. In this case we are setting it up to run a script.
  • Path to PHP – In this section, you must specify the path to PHP or Perl, depending on what type of script you are trying to run. If you don’t know the path to PHP, then you can easily ask your server admin. Here are some examples of possible paths to PHP: /usr/bin/php, /usr/local/bin/php
  • Flags for output – In this example I’ve used the –q flag which runs cron in quiet mode; this flag stops some unnecessary messages. You can also add /dev/null after the server path to stop error messages being output as well.
  • Server Path to your script – You will need to specify the full server path directly to your script. If you know the path to your account, then you can simply add the remainder of the path to your actual script eg. /home/user1/public_html might be the path to your account (document root) and the script might be in sendstudio/users resulting in /home/user1/public_html/sendstudio/users/scriptname.php


Setting Up Cron to Run SendStudio Files
Now that we know what the different parts of the command line actually do, we can begin to setup a cron job for a real use. In SendStudio, we need to run the sendemails.php (for sending newsletters) and sendautos.php (for autoresponders) in the “users” folder of the sendstudio application.

1. Let’s find the path to your installation of PHP. Type which PHP in a command line or just ask your host what it is.

2. Let’s find the path to your script. Find out your document root to your site, then add the path to the script. Eg. /home/user1/public_html/sendstudio/users/sendemails.php and /home/user1/public_html/sendstudio/users/sendautos.php. If you don’t know what your document root is, then you can either use a PHP info file to look for your document root.

3. Decide what frequency you wish to run the script. For every hour on the hour, you would use 0 * * * *

4. Create the cronjob. Login to telnet or your control panel and type crontab -e at the command line. This will let you edit your crontab file. Enter 0 * * * * /bin/php –q /home/user1/public_html/sendstudio/users/sendemails.php 0 * * * * /bin/php –q /home/user1/public_html/sendstudio/users/sendautos.php

5. Sit back and let cron do the rest.



Setting Crontabs Through Your Hosting Control Panel
Some ‘frontends’ to cron allow you to set the time and the command line for crontabs through your online hosting control panel. Look below for some examples of Cpanel and Plesk.

Cpanel 9 (Xskin)

Future versions of the .NET Framework (already in developement) bring even more features and even more power to our finger tips to help with desktop and web development.

Login to your control panel and look for the cronjobs icon There are two ‘modes’ to set cron jobs in Cpanel – you can choose either the standard or the advanced mode.


The standard mode looks like this:



This is a very simple way to add the frequency of the cron without needing to know what the asterisk or the numbers mean. In the command line you then add your path to PHP and the full path to your script. In the top field, you place the email address where the output will be sent – this is usually to alert you of errors etc.

In advanced mode you will see the following:

.

In this mode, you simply place the full command and then enter your frequency in the corresponding columns – they are clearly labeled so you can see which column are using as well.

Plesk (Reloaded)

Login to your Plesk control panel and look for the crontab icon

 

Crontabs are called ‘tasks’ in Plesk. In order to create a task click on the add new task icon. All current tasks will be shown in the tasks section for the specific user(in the dropdown).You can enter the email for cron output for this user in the email address field.



Here is the add new task screen



All fields are clearly marked so you know what time factor you are dealing with. Then just add your command to the command field and the task is complete.


Conclusion
Don’t be scared of cron. As you can see, it’s not so bad and most online control panels give you the tools necessary to quickly add crontabs to your scripts, to allow you to automate your business.

You don’t need to know much about UNIX or where the crontab files are created (all the hard core backend stuff) – just let the techies be fascinated by this stuff. If you just find the path to PHP and your document root, then everything else will fall into place.


About the Author

Joseph De Araujo is the Interspire Photoshop and Illustrator guru. Joseph designs most of the free Interspire web templates and also performs WebEdit Professional product support.


 
 


Home | Company | Support | Testimonials | White Papers | Affiliates | The Interspire Guarantee
Our Customers | Sitemap | Forum | Contact Us | Search | Privacy

© Copyright 2005. Interspire Pty. Ltd. ACN: 107 422 631