Adminbuntu

Everything for the Ubuntu Server Administrator

User Tools

Site Tools


Sidebar

Server Administration


Server Applications


At the Command Line


Elsewhere


Copyright 2013 Applied Conscious Technologies, LLC

Terms of Agreement

Contact


submit to reddit

cron

Cron

Cron is the Linux time-based job scheduler. It allows commands to be executed on a schedule and is useful in server administration for many purposes.

List All Crontab on System Perl script that lists all crontab entries for all users on the system. Can be used to quickly determine what jobs are scheduled to run.

cron manpage: http://manpages.ubuntu.com/manpages/precise/man8/cron.8.html

crontab manpage: http://manpages.ubuntu.com/manpages/precise/man5/crontab.5.html

cron howto: https://help.ubuntu.com/community/CronHowto


Crontab

The each user's cron table is maintained by the crontab command. User's cron tables are stored in the /var/spool/cron/crontabs directory in a file named the corresponding username. For example root's cron table is stored in /var/spool/cron/crontabs/root. Don't edit these files directly, use the crontab command.

Each line of a crontab file represents a job, and is composed of a CRON expression, followed by a shell command to execute.

crontab manpage: http://manpages.ubuntu.com/manpages/precise/man1/crontab.1.html

List the Cron Table

The -l (lowercase L) switch is used to list the cron table for the logged in user.

crontab -l

Edit the Cron Table

The -e switch is used to edit the cron table for the logged in user.

crontab -e

Crontab Entries

Lines in the crontab entries each represent one cron job and have six fields, 5 time value fields and 1 command field:

field values
Minute 0-59
Hour 0-23
Day of the month1-31
Month of the year1-12
Day of the week0-6 where 0 is Sunday. Or sun, mon, tue, wed, thu, fri, sat
Command To be executed by the sh shell

Fields are separated by white space (space and tab characters).

Each of the 5 time value fields can specify

  • individual value
  • a range of values, such as 0-10 or sun-wed
  • comma-separated list of individual values and ranges, like 0,20,40 or sun,wed,fri

Command Interpreter

Commands are executed by the sh shell, which is a more basic shell than bash with different functionality.

sh manpage: http://manpages.ubuntu.com/manpages/precise/man1/sh.1posix.html

Crontab Comments

Starting a line with a # character makes a comment, which are ignored by cron and useful for humans.

Special Characters

character name fields allowed description
* Asterisk all An asterisk means that the cron expression matches for all values for that field. For example, an asterisk in the 4th field (month) means every month.
/ Slash all Slashes describe increments of ranges. For example 3-59/15 in the 1st field (minutes) indicate the third minute of the hour and every 15 minutes thereafter. The form “*/…” is equivalent to the form “first-last/…”, that is, an increment over the largest possible range of the field.
% Percent all The % character is interpreted as a newline. If you want to use a % character, precede with a slash: /%.
, Comma all A comma is a list item separator within a field. For example, mon,wed,sat in the fifth field (day-of-week) means Monday, Wednesday and Saturday.
- Hyphen all Hyphens between two values indicate a range. For example, 0-23 in the hour (2nd) field means each hour of the day.
# Hash day-of-week '#' is allowed for the day-of-week field, and must be followed by a number between one and five. It allows you to specify constructs such as “the second Friday” of a given month. Note that the # hash character is also used to delineate comments when it is the first character on a line.

Special Values

The following special values can be used instead of the first five fields.

value meaning
@reboot Run once, at startup
@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 * * * *”

Command Output Mailed to User

Output from commands is emailed to the user that owns that crontab file.

To avoid receiving messages, append this to the end of the command in a crontab entry:

>/dev/null 2>&1

Crontab Examples

Every Day at 2 AM

This executes /home/cron/myscript every day at 2 AM.

# Run /home/cron/myscript at 2 AM every day.
0 2 * * * /home/cron/myscript
field value meaning
1 0 At the top of the hour.
2 2 At 2 AM.
3 * Every day of the month.
4 * Every month of the year.
5 * Every day of the week.
6 /home/cron/myscript This script will be executed

Every Hour During Work Hours

This executes /home/cron/myscript every work day on the hour, during work hours.

0 8-18 * * * /home/cron/myscript
field value meaning
1 0 Top of the hour.
2 8-18 Every hour from 8 AM to 6 PM.
3 * Every day of the month.
4 * Every month of the year.
5 * Every day of the week.
6 /home/cron/myscript This script will be executed

Twice Per Day

This executes /home/cron/myscript every day at 2:30 AM and 2:30 PM.

30 2,15 * * * /home/cron/myscript
field value meaning
1 30 30 minutes past the hour.
2 2,15 2 AM and 2 PM.
3 * Every day of the month.
4 * Every month of the year.
5 * Every day of the week.
6 /home/cron/myscript This script will be executed

Every 5 Minutes

This executes /home/cron/myscript every 5 minutes.

*/5 * * * * /home/cron/myscript
field value meaning
1 */5 Every 5 minutes – Every minute evenly divisible by 5.
2 * Every hour of the day.
3 * Every day of the month.
4 * Every month of the year.
5 * Every day of the week.
6 /home/cron/myscript This script will be executed

Other Cron Directories

/etc/cron.XXX Directories

  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.monthly

Separate from the crontab system, there are three directories on a standard Ubuntu server configuration used to schedule jobs.

By placing a script, with execute permissions set in one of the following directories, your job will be run on that scheduled basis.

If you look in the /etc/cron.daily directory, you will see that the standard Ubuntu server configuration has several jobs set to run daily.

/etc/cron.d/ Directory

The /etc/cron.d/ directory is also monitored for changes and jobs there are scheduled for execution. All files in /etc/cron.d/ must be owned by root and not not have to be executable.

The *cron manpage states that the crontab utility should be used instead of creating cron jobs in /etc/cron.d/.

/etc/crontab/ Directory

Cron also executes jobs in the /etc/crontab/ directory. Jobs in this directory use a different format than those maintained with the crontab utility.

Use the crontab utility to maintain jobs instead of using /etc/crontab/.

At Command for One-time Jobs

at man page: http://manpages.ubuntu.com/manpages/precise/man1/at.1.html

When you need to run a job one time, at a scheduled time, the at command can be used.

Run a Job at 3 AM Tomorrow

The time parameter can accept a wide variety of values. This example runs /home/username/myscript.sh at 3 AM the next day.

at -f /home/username/myscript.sh 3am tomorrow
option description
-f File to execute.

Time Expressions

To see the specification for possible time values see /usr/share/doc/at/timespec.

less /usr/share/doc/at/timespec

Some Time Expression Examples

  • 2:00 thursday
  • 16:00 next week
  • 8pm tomorrow
  • 6am june 16

cron.txt · Last modified: 2015/05/31 21:20 (external edit)