We removed our free Sandbox April 25th.
You can read more on our blog.

Periodic Tasks

You can run actions at periodic intervals. This can be minutes, hours, days or even months. Periodic tasks can be:

  • Database maintenance (e.g: compacting);
  • Backups;
  • Bulk E-mails (e.g: notifications, daily digest);
  • A program polling a third party service to update your database...

Using cron

Periodic tasks are run by cron, a background process that keeps track of a list of jobs to execute at specified intervals. The list is written in a file called a crontab with one job per line. For example, here is an hypothetic crontab that execute a program everyday a 18:42 PM:

42 18 * * * ~/current/jobs/fetch-picture-of-the-day

Each line has five fixed fields plus a command, here is how it is laid out:

minute hour day-of-month month day-of-week command

Fields are separated by blanks or tabs. The command may be one or more fields long. The allowed values for the fields are:

Field Allowed Values
minute * or 0 to 59
hour * or 0 to 23
day-of-month * or 1 to 31
month * or 1 to 12 or a name (see below)
day-of-week * or 0 to 7 or a name (0 or 7 is Sunday)
command text

You can use range of numbers: a range is two numbers separated by a ‘-‘ and is inclusive. For example “3-5” in an hour field specifies execution at hours 3, 4 and 5. An asterisk (“*”) is short form for a range of all allowed values.

Step values can be used in conjunction with ranges: following a range with “/number” specifies skips of number through the range. For example, “0-23/2” can be used in the hour field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say “every two hours”, just use “*/2”.

Fields can also be lists: a list is a set of numbers or ranges separated by commas. For example: “1,2,5,9”, “0-4,8-12”.

Names can be used in the month and day-of-week fields. Use the first three letters of the particular day or month. However, ranges or lists of names are not allowed.

Once written the crontab must be “registered” in cron to take effect, this is done with the “crontab” command and the path to your crontab:

crontab ~/current/jobs/crontab

The best place to do this automatically is in the “postinstall” hook called each time your application is built on your services. This is explained in the hooks guide.

The crontab command can also be used to display the list of defined jobs and/or to remove the jobs from the list:

crontab -l # list jobs
crontab -r # remove jobs

Note

Remember, do not forget to set the executable bit on your scripts with “chmod +x” (this is not needed on Windows).

You can find the original cron documentation here: (5)crontab.

Write Tasks In A Specific Language

The programs executed by cron can be written as plain shell scripts but could also be written in PHP (or even Python and Perl), do not forget to specify the interpreter to use in the first line of your program using a shebang:

PHP

#!/usr/bin/env php
<?php

    exit(0);

?>

Ruby

#!/usr/bin/env ruby

exit 0

Perl

#!/usr/bin/env perl

use strict;
use warnings;

exit 0;

Python

#!/usr/bin/env python

import sys

sys.exit(0)

Forward The Tasks Output

If you specify your E-mail address in your crontab, cron will use it to forward the jobs output to you:

MAILTO="louis+cron@company.com"
42 18 * * * ~/current/jobs/fetch-picture-of-the-day
21 9  4 * * ~/current/jobs/send-billing-reminder

Note

Sending E-mails from dotCloud requires some care please have a look at the sending E-mails page.