Chapter 8. Scheduled tasks

Table of Contents

8.1. The crontab format
8.2. Mailing the output

Each domain has the ability to run its own scheduled tasks via a file known as a crontab. This file enables jobs to be run on at specific times on specific days.

The format is the same as the well-known crontab file used on many Linux systems.

A domain’s crontab is found at config/crontab. For example, the crontab for my-brilliant-site.com would be found at /srv/my-brilliant-site.com/config/crontab.

8.1. The crontab format

The file is a list of jobs, one per line. Each line specifies first the times and days at which a job should run, followed by the command to run.

The first five fields, which are separated by spaces, specify the time and date at which the job should run. The rest of the line is interpreted as the command.

Field Allowed values

minute

0-59

hour

0-23

day of month

1-31

month

1-12 (or names, see below)

day of week

0-7 (0 or 7 is Sunday, or use names)

In addition an asterisk can be used to indicate for every allowed value. For example, to execute the command echo Hello Dave. at 18:40 every day, the crontab line would read as follows.

40 18 * * * echo Hello Dave.

Three-letter names can also be specified for use in instead of numbers for days of the week and months.

Weekdays
Sun, Mon, Tue, Wed, Thu, Fri, Sat
Months
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec

Any output generated by a command will be sent to the root account, unless specified otherwise. If no output is generated, no email will be sent.

The fields can be specified in the following ways:

  • As a comma separated list, e.g. 1,2,3,6
  • As a range, e.g. 1-3 would mean 1, 2, 3
  • As a range with a step, e.g. 0-30/2, would mean 0, 2, 4, 6 and so on until 30.
  • Or any combination of the above three, e.g. 1,2,10-12,20-24/2 would mean 1, 2, 10, 11, 12, 20, 22, 24.

Ranges can also be specified across "boundaries". For example 22-2 in the hour field will be interpreted as 22, 23, 0, 1, 2; Nov-Feb in the month field will mean 11, 12, 1, 2.

There is also a selection of shortcuts available:

  • @hourly — every hour, on the hour,
  • @daily or @midnight — every day at midnight,
  • @weekly — every week at midnight on Sunday,
  • @monthly — every month, at midnight on the first day of the month,
  • @yearly or @annually — every year, at midnight on 1st January.

The full crontab format is explained in more detail in the crontab (5) manual page.

8.2. Mailing the output

The output can be emailed to any recipient by specifying the MAILTO parameter at the top of the file.

For example, we would like to mail any output from our commands to bob@my-brilliant-site.com.

#
# send any output to Bob
#
MAILTO=bob@my-brilliant-site.com
#
# run at 9am every Monday - Friday
#
0 9 * * 1-5 wget http://www.my-brilliant-site.com/cron.php

If MAILTO is not set, or no recpient is specified, then they output will be sent to the domain directory’s owner, e.g. if /srv/my-brilliant-site.com/ were owned by admin, the output would get sent to admin@example.vm.bytemark.co.uk.