Difference between revisions of "Crontab"

From Christoph's Personal Wiki
Jump to: navigation, search
(Common mistakes)
Line 93: Line 93:
  
 
==== Common mistakes ====
 
==== Common mistakes ====
One common mistake is to use unescaped % in your command, you have to escape them
+
One common mistake is to use unescaped <code>%</code> in your command, you have to escape them
  # Error
+
  # WRONG:
  1 2 3 4 5 touch ~/error_`date "+%Y%m%d"`.txt
+
  1 2 3 4 5 touch ~/error_$(date "+%Y%m%d").txt
  # Right
+
  # Right:
 
  1 2 3 4 5 touch ~/right_$(date +\%Y\%m\%d).txt
 
  1 2 3 4 5 touch ~/right_$(date +\%Y\%m\%d).txt
  
Line 111: Line 111:
 
  59 1 1-7 4 0 /root/shift_my_times.sh
 
  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.
+
At first glance it might look like this will run the script <code>shift_my_times.sh</code> at 1:59 am 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.
+
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 1 April to 7 April in addition to every remaining Sunday in April.
  
 
Here is one way this can be rewritten:
 
Here is one way this can be rewritten:
 
  # Prepare for the daylight savings time shift
 
  # Prepare for the daylight savings time shift
  59 1 1-7 4 * test `date +\%w` = 0 && /root/shift_my_times.sh
+
  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 like this
+
  
 +
Another common error is putting a cron job to be run every two hours like this:
 
  # adds date to a log file
 
  # adds date to a log file
 
  * 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log
 
  * 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log
Line 127: Line 126:
  
 
The correct way of specifying a cron job would be to:
 
The correct way of specifying a cron job would be to:
 
 
  # runs the date command every other hour at the top of the hour
 
  # runs the date command every other 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
 
  0 0,2,4,6,8,10,12,14,16,18,20,22 * * * date >> /var/log/date.log

Revision as of 08:46, 18 September 2021

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

crontab is a command line tool 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.

Crontab commands

export EDITOR=vi # to specify a editor to open crontab file.
crontab -e 
Edit your crontab file, or create one if it doesn't already exist.
crontab -l 
Display your crontab file.
crontab -r 
Remove your crontab file.

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

Fields

# (Use to post in the top of your crontab)
# ------------- min (0 - 59)
# | ----------- hour (0 - 23)
# | | --------- day of month (1 - 31)
# | | | ------- month (1 - 12)
# | | | | ----- day of week (0 - 6) (Sunday=0)
# | | | | |
# * * * * * 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 example below.

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

Special words

If you use the first (minute) field, you can also put in a keyword instead of a number:

@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 * * * *

Leave the rest of the fields empty so this would be valid:

@daily /bin/execute/this/script.sh

or,

*/10 * * * * /bin/execute/this/script.sh 2>&1 >> /var/log/crontab.log

to save the contents or,

*/10 * * * * /bin/execute/this/script.sh 2>&1 > /dev/null

to throw away the contents.

Mailing the crontab output

By default cron saves the output in the user's mailbox (root in this case) on the local system. But you can also configure crontab to forward all output to a real email address by starting your crontab with the following line:

MAILTO="yourname@yourdomain.com"

If you'd rather receive only one cronjob's output in your mail, change the cronjob like this:

*/10 * * * * /bin/execute/this/script.sh 2>&1 | mail -s "Cronjob ouput" yourname@yourdomain.com

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 * * 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 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
# Right:
1 2 3 4 5 touch ~/right_$(date +\%Y\%m\%d).txt

Pay attention to single quotes

# Error. % is not protected from cron.
# The daemon emails message with information: /bin/sh: unexpected EOF while looking for `''
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. When would this command run?

# Prepare for the daylight savings 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.

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 1 April to 7 April in addition to every remaining Sunday in April.

Here is one way this can be rewritten:

# Prepare for the daylight savings 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 like this:

# 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 even hours in the day.

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

# runs the date command every other 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

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 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=""

Back up files/directories/etc

25 4 * * * (/bin/date && /usr/bin/rsync -va --delete /home/xtof/.ssh/ /data/arc/ssh/) >> /data/log/xtof-backups.log
0 3 * * * /bin/echo "$(/bin/date '+\%F'): $(/usr/bin/curl -s4 icanhazip.com)" >> /home/xtof/external-ip.log 2>&1
0 * * * * /bin/echo "$(/bin/date '+\%F \%H:\%M'): $(/bin/ping -c10 8.8.8.8 | /usr/bin/awk -F',' '/packet loss/{print $3}')" >> /home/xtof/ping-results.log 2>&1

See also

  • at: runs a job at a specified future time.
  • anacron: runs job on a periodic interval, anachronistically.

External links

Linux command line programs
File and file system management: cat | cd | chmod | chown | chgrp | umask | cp | du | df | file | fsck | ln | ls | lsof | mkdir | more | mount | mv | pwd | rcp | rm | rmdir | split | touch | tree
Process management: anacron | at | chroot | cron/crontab | kill | nice | ps | sleep | screen | time | timex | top | nice/renice | wait
User Management/Environment: env | finger | id | locale | mesg | passwd | su | sudo | uname | uptime | w | wall | who | write
Text processing: awk | cut | diff | ex | head | tac | tee | iconv | join | less | more | paste | sed | sort | tail | tr | uniq | wc | xargs | perl
Shell programming: echo | expr | unset Printing: lp
Communications:
inetd | netstat | ping | rlogin | traceroute
Searching:

find | grep/egrep/fgrep | strings

Miscellaneous:

banner | bc | cal | man | yes