Crontab for beginners

1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic by 0 people | Log in to rate

Ranked #14,446 in How-To, #152,884 overall

Crontab? Huh?

Cron is a time-based job scheduler in all UNIX-like computer operating systems. "Cron" is short for "Chronograph".

Cron enables users to execute commands or scripts automatically at a specified time and date. It is normally used for administrative commands, like indexing the computer, deleting or making new log files, or for running backup scripts. A common use for it today is checking email automatically and backing up important files and directories - normally everything under /home/.

Cron is driven by a crontab, a configuration file that specifies the shell commands to be run on the time and date specified.
Early versions of Cron made their services available only to the super-user of the system; this was the single-user version. With the release of the multi-user Cron, these services were extended to all users of the system.

How it works 

In laymans terms

The earliest versions of Cron were very simple. This is basically what they did:
1: Read /usr/etc/crontab
2: Determine if any commands are to be run at the current date and time and if so, run them
3: Sleep for one minute
4: Repeat from step 1

This version of Cron was basic and robust, but it also consumed resources whether it found any work to do or not, because it needed to check the crontab file every minute.

Soon, a new version of Cron was developed, which was slightly more complex:
1: Look for a .crontab file in the home directories of all users
2: For every file found, determine the time in the future that each command is to be run
3: Place those commands on the queue
4: Enter the main loop:

1: Examine the command at the top of the queue and determine how far in the future it is to be run
2: Wait for that length of time
3: Execute the task at the top of the queue with the privileges of the user who made it
4: Determine the next time to run this command and put it back in the queue with that time

How to use it 

Crontab syntax

The crontab file is the hardest part of learning how to use Cron. It's not that hard to learn, so that will make the rest seem like cake.

The syntax is as follows.
Firstly, every line in the crontab file that begins with a hash - # - is a comment and can be safely removed - if you really want to.

The syntax is 5 integers, followed by a full path to a command.
A full path example would be /home/john/backup
In place of a digit can be an asterisk - * - signifying that all values should be used. It should be noted that all times are in 24-hour format.
The digits are as follows
1: Minute (0-59)
2: Hour (0-23)
3: Day of the month (1-31)
4: Month of the year (1-12)
5. Day of the week (0-6) (0=Sunday)
So 15 15 * * * would be every day at 3:15PM. Note the 24-hour times.
30 23 1 12 * would be December the 1st, at 11:30PM.

For example, if the line is
* * * * * /home/john/hello
Then /home/john/hello would be run every minute, every hour, every day. Extreme.

If the line is
00 12 * * * /home/john/hello
Then /home/john/hello would be run at midday, every day.

Some more examples are as follows

30 * * * * # Runs at 30 minutes past the hour, daily
45 6 * * * # Runs at 6:45AM every day
45 18 * * * # Runs at 6:45PM every day
00 1 * * 0 # Runs at 1:00AM every Sunday
00 1 * * 7 # Runs at 1:00AM every Sunday
30 8 1 * * # Runs at 8:30AM on the first day of every month
00 23 * * 0-7/2 # Runs at 11PM every other day


There are also some special keywords which you can use instead of specifying particular times.
@reboot # Runs at boot time
@yearly, @annually # Runs once a year
@monthly # Runs once a month
@weekly # Runs once a week
@daily, @midnight # Runs once a day
@hourly # Runs once an hour

Books from Amazon 

Guides and howtos

Both of the following books cover how to use crontab, probably in more extensive detail than shown here.

Automating Linux and Unix System Administration, Second Edition (Expert's Voice in Linux)

Amazon Price: $31.49 (as of 01/05/2010) Buy Now

Unix in a Nutshell, Fourth Edition

Amazon Price: $23.07 (as of 01/05/2010) Buy Now

by JSmith_RDDX

I'm John and I love technology! I'm a computer programmer and I use Linux. That's basically me. (more)

Explore related pages

Create a Lens!