Crontab
From DssWiki
{{DISPLAYTITLE:{{#if:|:|}}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.
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 |
[edit] 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.
[edit] Operators
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.
[edit] 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:
- For "day of the week" (field 5), both 0 and 7 are considered Sunday.
- 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.
[edit] Examples
[edit] 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 * * 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 &
[edit] 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 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
- 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.
[edit] 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 to null while redirecting stderr to stdout, so you receive mail only if errors 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=""
[edit] Enabling the jobs
To actually schedule the jobs you must submit them to the cron daemon using the command crontab. 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.
[edit] See also
- at: runs a job at a specified future time.
- anacron: runs job on a periodic interval, anachronistically.
- cron
- launchd: Mac OS X/Darwin cron replacement.
- List of Unix programs
[edit] External links
- Documentation
- Software
- Cron for Windows
- CVSweb for FreeBSD's cron - Paul Vixie's 1993 Vixie cron 3.0 release with some bugfixes applied
- fcron - An enhanced replacement for vixiecron and anacron (GPL)
{{#if: |
Unix command line programs and builtins (more) | |||
| File and file system management: | cat | chattr | cd | chmod | chown | chgrp | cksum | cmp | cp | du | df | file | fsck | fuser | ln | ls | lsof | mkdir | mount | mv | pwd | rm | rmdir | split | touch | ||
| Process management: | at | chroot | crontab | exit | kill | killall | nice | pgrep | pidof | pkill | ps | sleep | time | top | wait | watch | ||
| User Management/Environment: | env | finger | id | mesg | passwd | su | sudo | uname | uptime | w | wall | who | whoami | write | ||
| Text processing: | awk | comm | cut | ed | ex | fmt | head | iconv | join | less | more | paste | pg | sed | sort | tac | tail | tr | uniq | wc | xargs | ||
| Shell programming: | basename | echo | expr | false | printf | test | true | unset | Printing: | lp |
| Communications: inetd | netstat | ping | rlogin | traceroute | Searching: find | grep | strings | Miscellaneous: banner | bc | cal | dd | man | size | yes | |
