Tag Archives: cron

Decoding Cron

Cron is a useful feauture on Linux operating systems that allows easy scheduling of repeated tasks. Typically this includes rotating log files so that they don’t get to big, and checking for updates to software.

Cron commands can look intimidating to the non-technical user, so this article explains how to read them.

A typical crontab line looks something like this:

a typical crontab line

The asterisks at the beginning of the line are the all important timing information.

 

Decoding Cron

The values accepted for each field are:  The minute field value must be 0-59, the hour field 0-23, the day of month field 1-31, the month field 1-12 and the day of week field 0-6 (Sunday is 0 but this can also be given as 7).

The values can be given in a variety of formats:

  • An asterisk (*) character will match all possible values for the field: e.g. the Cron expression “* * * * *” will run the command every minute since this is the smallest representable time period.

 

  • A literal value: e.g. “30 * * * *” will run the command whenever the minute is 30, i.e. once an hour at half-past the hour.
    “* * 5 * *” will run every minute when it is the 5th day of the month.

 

  • A list is given by separating each possible value using a comma: e.g. “0,15,30,45 * * * *” will run the command whenever the minute is either 0, 15, 30 or 45. Another example, “0 1,2,3 * * *” will run the command between 1am and 3am (inclusive) but only when the minute is 0, i.e. on the hour. Lists can also contain ranges (see below).

 

  • A range is given by separating the lower and upper values of the range with a hyphen (-): e.g. “0 1 1-5 * *” will run the command at 1am on the first, second, third, fourth and fifth days of the month.

 

  • An increment is given by using a forward slash: e.g. “*/15 * * * *” will run the command every 15 minutes starting on the hour. i.e. this example is the same as the list example given above to run whenever the minute is 0, 15, 30 or 45.
  • A final example “35 7 * * *” means run the command at 35 minutes past 7am, everyday.

By default each run of the command will write to your log file. A couple of options are to change this:

  • Add “>> /dev/null 2>&1” to the end of the command line (a space then the text inside the “)
  • Add “| mail -s “Subject” me@domain.com” to send a confirmation by email.

Ready to have a go?

This utility helps you build Cron expressions easily by choosing job scheduling scenarios. The crontab entries produced work with Vixie Cron, popular in many Linux distributions.

In the examples above, you should normally remove the inverted commas (“) around the commands.

If you need any advice about cron, we’ll be happy to help. Please contact us.