Automating Repetitive Tasks with Cron Jobs

This chapter will help you execute repeatedly and automatically arbitary UNIX commands at any time you specify.

Tip

This is an advanced topic, it requires some knowledge, e.g. how to write a UNIX command. You only need to read this chapter if your site is complex, with lots of administrative tasks.

Click the "Cron Jobs" link in the "Tools" group in the Control Panel home screen. Your screen should then look like this:

Handling Jobs' Output

The cron jobs are non-interactive programs that usually execute unattended. In order to find out later if the task did its work successfully you should log all activity of the job. For simpler jobs managing your own log files is not practical. You can simply mail the output of the job to yourself. In order to do that input an email address in the "Send jobs output to:" box and click the "Set" button. The output of all your cron jobs will then be mailed to that address. If you don't wish to receive output from your jobs leave the box blank and click the "Set" button. The output of your jobs will then be discarded.

Viewing and Managing Active Jobs

In the "Installed Cron Jobs" table you should see a list of all your active cron jobs (if any). The "Period" column specifies when each cron job is executed. The period contains five elements. Each element can be an asterisk - "*", a number or a day of week or month abbreviation. Here is a short description of each element of a period:

  1. Minute - Specifies the minute when the respective task is to be executed. Can take values from 0 to 59

  2. Hour - Specifies the hour when the task is to be executed. Can take values from 0 to 23.

  3. Day of Month - The day of the month. Possible values: 0-31

  4. Month - One of the following values: jan, feb, mar, apr, jun, jul, aug, sep, oct, nov, dec

  5. Day of Week - One of the following values: mon, tue, wed, thu, fri, sat, sun

Instead of specifying a value an asterisk can be used and it means "any". For example if the minutes element is 30 and the hour element is "*" then the job will be run at 0:30, 1:30, 2:30 and so on. You can also use expressions like */15 which means "run this tasks every fifteen minutes".

Note

Our system will refuse to accept periods that schedule tasks to run more than once in fifteen minutes.

So a task can be scheduled to be executed at arbitary intervals and at any time you like, for example every fifteen minutes or each monday or once a year in february, or every day at 5 a.m. - the combinations are endless.

To remove a cron job click the trash icon in the "Manage" column of the "Installed Cron Jobs" table.

Adding New Jobs

You can do this in the "New Cron Job" form. To add a new cron job you must specify first when it should be executed. You can use one of the predefined intervals:

or specify your custom period. If you use one of the predifined periods your job will execute that often but the exact moment in time when the job will run is unknown, e.g. if you schedule a job to run every day it will run every day at the same time but we reserve the right to determine at what time of day and to change this time as we see fit.

Tip

We recommend that you use one of the predefined intervals. It's simpler, easier and less error prone. And it also allows us to change the exact moments when your jobs run so that they run when the system is idle thus avoiding stress loads on the server which allows us to offer a better quality service.

If you choose a predefined period the rest of the fields in the period selection form are ignored. They are only taken into account if you choose "Custom" from the "Predefined Period" drop-down box. You can then specify manually each element of the period (see the previous section).

Once you've done selecting the period you should input a command in the "Command:" box. This can be any command you could enter on the command-line. You have the flexibility and power (but the complexity too ;-) of the standard UNIX shell. We'd recommend however if your command is complicated to put it a separate shell script and invoke the shell script as a cron job instead of having the complex command in your crontab.

Tip

We strongly recommend that you test the command you're planning to use prior to installing it as a cron job.