Automated cron jobs

All Anchor web hosting accounts come with the ability to run cronjobs, this page will give you some details on how they are controlled.

Firstly, a bit of background.

Under a Linux/Unix environment all automated tasks are completed using an application called 'cron' and these tasks are defined within an plain text which is commonly referred to as the 'crontab' (short for cron table).

In order to edit this file you will need to log into the web hosting server using SSH. Using SSH access is outside the scope of this document, however check out this page which gives full details on getting SSH access to your web hosting account

Configuring the actual cronjob is relatively trivial as once you logged in. The following command will load your existing crontab for editing:

crontab -e

If you haven't created a crontab file previously, then you can cut and paste the following template into the file:

MAILTO=username@domainname.com.au
# ------------- min (0 - 59)
# | ----------- hour (0 - 23)
# | | --------- day of month (1 - 31)
# | | | ------- month (1 - 12)
# | | | | ----- day of week (0 - 6) (Sunday=0)
# | | | | |
# * * * * * command to be executed

Once this has been done, the very first thing you need to set is setting the MAILTO field correctly. This is very important as it ensures that any output (such as errors) are emailed to a location where you will see them. To set this, you just need to change username@domainname.com.au to a valid email address.

After this has been completed then you are ready to add the scheduled jobs to the file.

The format can be little bit confusing to start off with, but is actually really simple and is probably best described through using some examples.

Example 1

In this example I have a script called 'automated_task' in the users home directory 'bob' which I wish to run once a day at 5am.

The following line would need to be added to the file:

0 5 * * * /home/bob/automated_task

As you will note:

  • The minutes column is set to 0 - Run on the hour (ie, when minutes is 00)
  • The hour column is set to 5 - Run when the hour is 5 (ie, 5AM in the morning, 5pm would be 17)
  • All other time columns are set to * - Run when all other conditions are met.