Cron is a schedule based service that runs on just about every UNIX and Linux 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 what cron is and how you can use it to schedule the execution of the system’s scheduled sending script (so you can schedule your email campaigns to be sent at a later date or do bounce processing) on your 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 the system as the application that I am setting up the cron job for. The actual scheduled sending script will be used to send out emails and autoresponders.
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 terminal window.
Cron – Paint by Colors.
Lets 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’s an explanation of the above cron command:
Red: Minute of the hour – Set to a number between 0-59 for the corresponding minute of the hour or * for every minute
Orange: Hour of the day – Set to a number between 0-23 for the corresponding hour of the day or * for every hour
Yellow: Day of the month – Set to number between 1-31 for the corresponding day of the month or * for every day
Light Green: 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)
Dark Green: 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)
Cyan: Beginning of the command line – Here you specify the path to PHP or Perl and any flags you might need.
Blue: 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 asterisks specifie that the script should be run on every occurrence 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 – The application will attempt to read this from your server information. If you do not see a path to PHP in your command then you will have to contact your host to determine what this is.
Flags for output – In this example I’ve used the -f flag which specifies the PHP file you want to run. You can also add > /dev/null after the server path to stop error messages being output.
Server Path to your script – You will need to specify the full server path directly to your script. You can find this on the ‘Cron Settings’ tab from the settings page, as shown below. If you can’t find the ‘Cron Settings’ tab then you need to first enable scheduled sending. This is done by selecting the ‘Do You Want to Enable Cron Support?’ shown in the image below.
Setting up cron to run the scheduling script.
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 the system, we need to run the ‘admin/cron/cron.php’ file to be able to make use of the scheduled emails, autoresponders and automatic bounce handling.
Decide what frequency you wish to run the script. For every minute, you would use * * * * *
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. Type this text in:
If you set your cron to send every minute on your server you can make full use of the in built functionality to be able to delay your running of emails, autoresponders and bounce processing.
Running the scheduling script regularly.
You can run the scheduling script as often as you like and the more frequently you run the script the more precision you can have with your tasks. The settings inside the application’s control panel control how often a particular task is run. For example, you can run the scheduling script every 2 minutes, then use the settings in the application’s control panel to only run autoresponders every 15 minutes. This allows you to control everything from inside the application and you don’t need to change your cron settings in your hosting control panel.
Setting Crontabs Through Your Hosting Control Panel.
Some ‘front ends’ 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.
Login to your control panel and look for the cronjobs icon.
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.