Automate Your Life With Cron

Using Cron

Cron is driven by a crontab file, which contains instructions to the cron daemon telling it to run some command at a specified date and time. Each user on the system has their own crontab and the commands therein are run as the respective user. Additionally, some systems contain system-wide crontab files, which are accessible only to administrators.

Anatomy Of A Cron Job

Each line in the crontab file represents a job and consists of five time and date fields followed by a command. System-wide crontab entries also contain the user name (as well as optional values for group and login class) between the five time and date fields and command. The time and date fields are as follows:

Field Allowed Values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names)
day of week 0-7 (0 or 7 is Sun, or use names)

Each field may also contain special characters, the most useful of which are listed below:

Character Definition Example Explanation
Asterisk (*) Indicates all values of the field 0 * * * * command Run command every hour
Slash (/) Describes increments 0 */2 * * * command Run command every 2 hours
Comma (,) Separates items of a list 0 0 * 1,2,3 * command Run command every day in Jan, Feb, & Mar
Hyphen (-) Used to define a range 0 8-17 * * * command Run command every hour from 8:00 a.m. to 5:00 p.m.

The time and date fields may also be represented by eight special predefined strings:

String Meaning Equivalent To
@reboot Run once, at startup (N/A)
@yearly Run once a year 0 0 1 1 *
@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 Run once a day 0 0 * * *
@midnight Run once a day 0 0 * * *
@hourly Run once an hour 0 * * * *

This may all seem a bit overwhelming at first, but all will be made clear as soon as we work through the first example. Let's get started!



;