Cron

From Colettapedia
Jump to navigation Jump to search

General

  • Note that comments are not allowed on the same line as cron commands, since they will be taken to be part of the command. Similarly, comments are not allowed on the same line as environment variable settings.
  • An active line in a crontab will be either an environment setting or a cron command.
  • Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
  • Disable email by putting the following at the end of cmd line: >/dev/null 2>&1
  • Generate cron execution log file by '>' piping output to a file

Crontab Commands

  • 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 -v = Display the last time you edited your *crontab file. (This option is only available on a few systems.)

Crontab Environment Values

  • SHELL = set by default to be /bin/sh, can be overridden
  • LOGNAME = set from the /etc/passwd line of the crontab´s owner, CANNOT be overridden
  • HOME = set from the /etc/passwd line of the crontab´s owner, can be overridden
  • MAILTO = If defined, send mail as a result of running commands in "this" crontab. If defined but empty (MAILTO=""), no mail will be sent. Otherwise mail is sent to the owner of the crontab.

Files

  • In Fedora:
  1. /etc/crontab = system crontab file
  2. /etc/cron.hourly/
  3. /etc/cron.daily/
  4. /etc/cron.weekly/
  5. /etc/cron.monthly/
  6. /etc/cron.d/ = Jobs in cron.d are system jobs, which are used usually for more than one user. That’s the reason why is name of the user needed. MAILTO on the first line is optional.
  7. /etc/cron.allow = if exists, only listed users can use the crontab command
  8. /etc/cron.deny = if exists, listed users are disallowed to use the crontab command
  9. /etc/sysconfig/crontab
  10. /var/spool/cron/
  11. /var/spool/anacron/

Crontab file syntax

  • Each line has five time and date fields, followed by a user name if this is the system crontab file, followed by a command.
  • format: [min 0-59] [hour 0-23] [day of month 1-31] [month 1-12, or names] [day of week 0-7, 0 or 7 is Sun, or use names]
  • Use the first three letters of the particular day or month, case insensitive. Ranges or lists of names are not allowed.
  • ranges 0-7, lists 5,6,7 are allowed
  • every other hour = */2

Sample crontab file

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

Another Sample crontab file

# use /bin/sh to run commands, no matter what /etc/passwd says
SHELL=/bin/sh
# mail any output to ‘paul’, no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * *     $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5    mail -s "It’s 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun     echo "run at 5 after 4 every sunday"