FACTOID # 149: Norwegians consume more than 15 times as much coffee per person as the Irish.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Crontab

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. Filiation of Unix and Unix-like systems Unix (officially trademarked as UNIX®) is a computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie and Douglas McIlroy. ... Diagram of the relationships between several Unix-like systems A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification. ... It has been suggested that Maintenance OS be merged into this article or section. ... Command has multiple meanings: An order. ... The standard streams are a set of input and output channels featured in Unix and Unix-like operating systems, and provided by the standard I/O library (stdio. ... For other uses, see Chronos (disambiguation). ...


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. In Unix and other computer multitasking operating systems, a daemon is a computer program that runs in the background, rather than under the direct control of a user; they are usually instantiated as processes. ...

Contents

crontab files

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.


Operators

There are several ways of specifying multiple date/time values in a field: In computer science and computer programming, system time represents a computer systems notion of the passing of time. ...

  • 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.


Fields

 # 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, though some versions of Unix such as AIX do not list "7" as acceptable in the man page.
  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. Almost all substantial UNIX and Unix-like operating systems have extensive documentation available as an electronic manual, split into multiple sections called man pages (short for manual pages and based on the command used to display them). ...


Examples

Crontab file for adm user on AIX system

 #================================================================= # 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,23 * * 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 & 

Common mistakes

  • One of the most common mistakes happens when 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 on some systems. 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 common user mistake is assuming that the environment variables are the same for the user shell and the process launched.
  • 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, insert on the first line DISPLAY=:0.0
    • when running your application, append --display :0.0, for example, "/usr/bin/audacious --display :0.0". The value :0.0 is given here 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:
 # Incorrect: 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 `'' Environment variables are a set of dynamic values that can affect the way running processes will behave on a computer. ...

 # Correct: 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_20060403.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:59 am on the first Sunday of April. This, however, is not correct. Though DST is common in Europe and North America, most of the worlds people do not use it. ...


Unlike all of the other fields the third and fifth fields are actually an OR operation. So it will run at 1:59 am 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 (not compatible with all crons) 0 */2 * * * date >> /var/log/date.log 
  • Other errors stem from permissions not being set correctly:

The crontab files are often found in the /var/spool/cron/ directory. If your user has no read permissions for /var/spool/cron/yourUserName, then the command crontab -e will produce an error. Similarly, if your user does not have write permissions for your crontab, you cannot save changes to the crontab.


Disabling email

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 and stderr to null, so you will not receive any mails:
 >/dev/null 2>&1 
  • You may also choose to receive mails only when an error occurs. In this case, only redirect stdout to null like this:
 >/dev/null 
  • 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="" 

The standard streams are a set of input and output channels featured in Unix and Unix-like operating systems, and provided by the standard I/O library (stdio. ... The standard streams are a set of input and output channels featured in Unix and Unix-like operating systems, and provided by the standard I/O library (stdio. ... The standard streams are a set of input and output channels featured in Unix and Unix-like operating systems, and provided by the standard I/O library (stdio. ... Vixie cron is a cron daemon originally coded by Paul Vixie 1987. ...

Enabling the jobs

To actually schedule the jobs you must submit them to the cron daemon using the command contab. The easiest and most often used method is to issue the command crontab -e, which will open the default crontab file with a text editor. Once the user has made the desired changes, saving and quitting that file will automatically enable the jobs. Alternatively, one can first generate a file with the desired configuration, and then specify that file to crontab. The latter is achieved by executing crontab filename, where "filename" is the name of the file we just created.


See also

The at command is used to schedule commands to be executed once at a particular time in the future. ... Anacron is a computer program written by Sean Perry for the Unix operating system. ... cron is a time-based scheduling service in Unix and Unix-like operating systems. ... launchd is a unified, open source service management framework for starting, stopping and managing daemons, programs and scripts. ... Mac OS X (official IPA pronunciation: ) is a line of proprietary, graphical operating systems developed, marketed, and sold by Apple Inc. ... Darwin is a free and open source, Unix-like operating system first released by Apple Inc. ... This is a list of Unix programs. ...

External links

  • Documentation
    • Open Group's crontab specification - official UNIX 03
    • Running Server-side Scripts in Cron Jobs
  • Software

  Results from FactBites:
 
Crontab - Wikipedia, the free encyclopedia (985 words)
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 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).
crontab (521 words)
Crontab is the program used to install, deinstall or list the tables used to drive the cron daemon in Vixie Cron.
Each user can have their own crontab, and though these are files in /var, they are not intended to be edited directly.
Note that su can confuse crontab and that if you are running inside of su you should always use the -u option for safety's sake.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m