Command scheduling with cron crontab

From Cosmin's Wiki

Jump to: navigation, search

Home > Linux how to's > Command scheduling with cron crontab

What is a Crontab?

A cron is a utility that allows tasks to automatically run in the background of the system at regular intervals by use of the cron daemon. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at what times they are to be run. This can be quite useful. For example, you may have a personal temporary directory that you wish to be cleaned out once a day to keep your quota from being exceeded. This is where cron scheduling comes in to play.

In Debian the cron package is installed as part of the base system, and will be running by default.

Cron, as supplied in Debian, has two purposes:

  • To run system jobs on a daily/weekly/monthly basis
  • To allow users to setup their own schedules

The system schedules are setup when the package is installed, via the creation of some special directories:


Using Crontab

The normal manner which people use cron is via the crontab command. This allows you to view or edit your crontab file, which is a per-user file containing entries describing commands to execute and the time(s) to execute them.

To display your file you run the following command:

crontab -l

root can view any user's crontab file by adding "-u username", for example:

crontab -u cosmin -l  # List cosmin's crontab file.

A cron schedule is a simple ASCII text file. As said before, each user has his own cron schedule. This is normally located in /var/spool/cron/crontabs for linux machines. The crontab files are not edited (or created) directly and you do not have access to the file without invoking it from the crontab command. You may not use any text editor you wish. You must use the text editor that has been specified in you system variables (see your system administrator for these). The text editor vi is usually the default text editor. The editor must be invoked using the -e switch. To create a cron schedule type:

crontab -e

Each cron job has at least 6 sections. Each section is separated by a single space, but the final section may have spaces within it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are indicate when and how often you want the task (the sixth position) to be executed. All time are local times not GMT.

The format of these files is fairly simple to understand. Each line is a collection of six fields separated by spaces.

The fields are:

  1. The number of minutes after the hour (0 to 59)
  2. The hour in military time (24 hour) format (0 to 23)
  3. The day of the month (1 to 31)
  4. The month (1 to 12 or use names)
  5. The day of the week(0 or 7 is Sun, or use name)
  6. The command to run

More graphically they would look like this:

# +---------------- 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

Each of the first five fields contains only numbers, however they can be left as '*' characters to signify any value is acceptable.

Note: If the computer system running the crontab is down, the crontab will not run as well. When the system comes back up, the crontab will resume its normal activity, but will not go back and run the jobs that were missed due to the system being down.

Instead of the first five fields, one of eight special strings may appear:

@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".


Let's take some examples, which are commented in the snippet below:

# Run the `something` command every hour on the hour
0   *   *   *   * /sbin/something
# Run the `nightly` command at ten minutes past midnight every day
10  0   *   *   * /bin/nightly
# Run the `monday` command every monday at 2 AM
0   2   *   *   1  /usr/local/bin/monday
# Use a range of hours matching 1, 2, 3 and 4AM
*   1-4   *   *   * /bin/some-hourly
# Use a set of hours matching 1, 2, 3 and 4AM
*   1,2,3,4   *   *   * /bin/some-hourly
#Run command at 8:00am,10:00am and 2:00pm every day
00 8,10,14 * * * do_something.script
#Run command every 5 minutes during market hours
*/5 6-13 * * mon-fri get_stock_quote.script
#Run command every 3-hours while awake
0 7-23/3 * * * drink_water.script