Crontab

From Wikipedia, the free encyclopedia

The correct title of this article is crontab. The initial letter is shown capitalized due to technical restrictions.

The crontab command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. It reads a series of commands from standard input and collects them into a file also known as a "crontab" which is later read and whose instructions are carried out. The name is derived from Greek chronos (χρόνος), meaning time.

Generally, the schedules modified by crontab are enacted by a daemon, crond, which runs constantly in the background and checks once a minute to see if any of the scheduled jobs need to be executed. If so, it executes them. These jobs are generally referred to as cron jobs.

Contents

The crontab files are where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a systemwide crontab file (usually in /etc or a subdirectory of /etc) which is also used but can only be edited by the system administrator(s).

Each line of a crontab file follows a particular format as a series of fields, separated by spaces and/or tabs. Each field can have a single value or a series of values.

There are several ways of specifying multiple date/time values in a field:

  • The comma (',') operator specifies a list of values, for example: "1,3,4,7,8"
  • The dash ('-') operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6"
  • The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour'..

There is also an operator which some extended versions of cron support, the slash ('/') operator, which can be used to skip a given number of values. For example, "*/3" in the hour time field is equivalent to "0,3,6,9,12,15,18,21"; "*" specifies 'every hour' but the "/3" means that only the first, fourth, seventh...and such values given by "*" are used.

# Use the hash sign to prefix a comment
# +---------------- 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

Notes:

  1. For "day of the week" (field 5), both 0 and 7 are considered Sunday.
  2. Counterintuitively, if both "day of month" (field 3) and "day of week" (field 5) are present on the same line, then the command is executed when either is true. See the examples below.

The sixth and subsequent fields (i.e., the rest of the line) specify the command to be run.

#=================================================================
#      SYSTEM ACTIVITY REPORTS
#  8am-5pm activity reports every 20 mins during weekdays.
#  activity reports every hour on Saturday and Sunday.
#  6pm-7am activity reports every hour during weekdays.
#  summary prepared at 18:05 every weekday.
#=================================================================
0,20,40 8-17 * * 1-5 /usr/lib/sa/sa1 1200 3 &
0 * * * 0,6 /usr/lib/sa/sa1 &
0 18-7 * * 1-5 /usr/lib/sa/sa1 &
5 18 * * 1-5 /usr/lib/sa/sa2 -s 8:00 -e 18:01 -i 3600 -ubcwyaqvm &

  • One of the most common mistakes involves creating a new cron job for testing purposes. When doing this, the run time must be at least two minutes into the future, as the tab only reloads at the next minute mark after being edited. For instance, if the time is now 12:05, the earliest you can schedule a job would be 12:07, as the tab would reload at 12:06:01. You can overcome this though by restarting your cron service for faster testing.
  • A mistake which cannot be found in the man page of crontab, is to launch an X Window Application from crontab. The problem is that crontab has no clue if you're running X or not; its main purpose is to run console commands. There are two solutions for this, in your crontab file, either insert on the first line DISPLAY=:0.0 or when running your application, append --display :0.0, for example "/usr/bin/audacious --display :0.0". The value :0.0 is there as an example, you can find yours by running, in console : "echo $DISPLAY".
  • Another common mistake is to use unescaped % in your command; you have to escape them:
# Wrong:
1 2 3 4 5 touch ~/error_`date "+%Y%m%d"`.txt

The daemon emails message with information: /bin/sh: unexpected EOF while looking for `''

# Right:
1 2 3 4 5 touch ~/right_$(date +\%Y\%m\%d).txt

# Also correct, with single quotes: 1 2 3 4 5 touch ~/error_$(date '+%Y%m%d').txt
# Overdosed. This touches something like ~/error_\2006\04\03.txt 1 2 3 4 5 touch ~/error_$(date '+\%Y\%m\%d').txt
  • Below is another common error:
# Prepare for the daylight saving time shift
59 1 1-7 4 0 /root/shift_my_times.sh

At first glance it might look like this will run the script shift_my_times.sh at 1:59am on the first Sunday of April. This, however, is not correct.

Unlike all of the other fields the third and fifth fields are actually an OR operation. So it will run at 1:59am each day from the April 1st to April 7th in addition to every remaining Sunday in April.

Here is one way this can be rewritten:

# Prepare for the daylight saving time shift
59 1 1-7 4 * test `date +\%w` = 0 && /root/shift_my_times.sh
  • Another common error is putting a cron job to be run every two hours:
# adds date to a log file
* 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log

The above will schedule the cron job to be run every minute of every even hour in the day.

The correct way of specifying a cron job would be to:

# runs the date command every even hour at the top of the hour
0 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log
# an even better way
0 */2 * * * date >> /var/log/date.log

If any output is produced by a command executed from a crontab, the cron daemon will normally email the user that output.

  • To silence any particular command, you may redirect its output to /dev/null. To stop receiving email output from crontab, append the following to any command. This will redirect stdout to null while redirecting stderr to stdout, so you receive no errors if they occur:
>/dev/null 2>&1
  • Under the commonly used Vixie cron, you can also turn off email notification for all of a particular user's cronjobs by adding this line to the beginning of their crontab:
MAILTO=""

To actually schedule the jobs you must submit them to the cron daemon using the command crontab. The easiest method is to first generate a file and specify that file to crontab, but you may also enter the commands directly into standard input. Once you have a file (e.g., example.crontab) the jobs are enabled by executing the command crontab example.crontab.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.