Setting Up Scheduled Sending Using Cron


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 exactly 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) 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 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.

Getting Started [top]

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.

Cron - Paint by Colors [top]

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:


A typical crontab command, explained below.

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


A color coded crontab command.

Here's 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? [top]

The Asterisk specifies 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 [top]

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. Setting up cron to run the scheduling script [top]

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. Running the scheduling script regularly [top]

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 [top]

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.

Cpanel 9 (Xskin) [top]

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:


Setting up a cronjob in Cpanel using the simple options.

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.

For the advanced mode you will see the following:


Setting up a cronjob in Cpanel using the advanced options.

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) [top]

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.


Setting up a cronjob in Plesk using the tasks section.

Here's the "Add New Task" screen:


The 'Add New Task' screen in Plesk's tasks section.

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.