Configuring cron
Home Up Configuring cron Configuring ssmtp

 

Configuring and using CRON

Installing cron:

root@zebidee:~# ipkg install cron
Installing cron (3.0pl1-r7) to root...
Downloading http://ipkg.nslu2-linux.org/feeds/slugos-bag/cross/3.10-beta
/cron_3.0pl1-r7_armeb.ipk
Configuring cron
Adding system startup for /etc/init.d/cron ...
/etc/rc2.d/S65cron -> ../init.d/cron
/etc/rc3.d/S65cron -> ../init.d/cron
/etc/rc4.d/S65cron -> ../init.d/cron
/etc/rc5.d/S65cron -> ../init.d/cron
/etc/rc0.d/K65cron -> ../init.d/cron
/etc/rc1.d/K65cron -> ../init.d/cron
/etc/rc6.d/K65cron -> ../init.d/cron
System startup links for /etc/init.d/cron already exist.
Stopping Vixie-cron.
Starting Vixie-cron.
root@zebidee:~# 

Using cron

cron looks for scheduled jobs in several places:

  1. in a text file /etc/crontab
  2. in any text files in the directory /etc/cron/
  3. in a text file stored by the crontab command, located in /var/cron/tabs and named after a username

I'll concentrate on 3 for now. 

The crontab command:

crontab -e

Runs a text editor, allowing you to edit your crontab file.

crontab -l

Lists the contents of your crontab file.

crontab -r

removes your crontab file.

crontab <filename>

replaces your crontab file with the contents of the named file.

crontab -

replaces your crontab file with input from stdin (best used with "pipes").

other options: -u <username> 

edits the crontab file belonging to another user. This option probably requires you to be "root".

The basic crontab file format

Each line consists of either:

  1. a comment line beginning with a #.
  2. an environment variable such as "MAILTO"
  3. a time and date followed by a command to be executed at that time and date
  4. a special symbol line beginning with an @ sign 

Comments:

You may want to paste the following at the beginning of your crontab for reference:

# (Use to post in the top of your crontab)
# ----------------- minute (0 - 59)
# |  -------------- hour (0 - 23)
# |  |  ----------- day of month (1 - 31)
# |  |  |  -------- month (1 - 12)
# |  |  |  |  ----- day of week (0 - 7) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *  command to be executed

Environment variables

MAILTO sets the destination for cron reports

MAILTO="<email address>"

I strongly recommend that you set the MAILTO variable as it ensures that the report will go where you expect. The default destination may well be wrong.

Time and date lines

A normal entry consists of five numbers giving the minute, hour, day, month, day of week followed by the command to be executed.

The numbers are separated by spaces NOT commas. An asterisk "*" denotes a wildcard so to run "command" every day at 6am you might write:

  0  6  *  *  *  command

As a general rule you should use the day of week OR day of month options but not both, if both are specified then cron will execute the command if either match.

You can match multiple values using commas, so to run the above job on Mondays, Wednesdays and Fridays write:

  0  6  *  * 1,3,5 command

You can match ranges using a dash so to run every weekday:

  0  6  *  * 1-5 command

You can modify a wildcard to match special intervals using a slash, so to run every three hours:

  0 */3 *  *  * command

You can combine conditions, so to run every three hours but only on weekdays:

  0 */3 *  * 1-5 command

Special lines:

Name: When it runs: Equivalent to:
@yearly or @annually   Run once a year.
0 0 1 1 *
@monthly   Run once a month.
0 0 1 * *
@weekly   Run once a week.
0 0 * * 0
@daily or @midnight Run once a day.
0 0 * * *
@hourly Run once an hour.
0 * * * *
@reboot   Run once, at startup. No equivalent

 The @reboot line is particularly interesting as it's a handy way to start a service without installing a start-stop script or run a service as a user other than root.

The master crontab format

By "Master" I am referring to the crontabs located in /etc

These are not edited by the crontab command so they are a good place to put crontab lines that start administrative tools that you do not want tampered with.

The format is similar to the above except an extra column is added between the date and the command giving the username that the command should run as eg:

  0 */3 *  * 1-5 root command

These commands can be put in a text file called /etc/crontab,
or in a text file in a directory /etc/cron/ (normally with the same name as the program it runs).

Please note: I could not get the @reboot special line to function when put in /etc/crontab