配置文件

字段意义

Mean Minute Hour Date Month Week Command
Range 0~59 0~23 1~31 1~12 0~7

字符用法

    • (星号)任意时刻都可以
  • , (逗号)分割时间段
    • (减号)一段时间的范围
  • /n (斜线)每隔 n 单位间隔

以下为手册汉化计划


一个 crontab 文件包含了 cron 守护进程的指导,看上去是这样的:运行这个命令在这个时间,在这个天
每个用户都可以定义自己的 crontab。
用户自己在 crontab 中定义的命令将会被执行。
Uucp and News usually have their own crontabs, eliminating the need for explicitly running su(1) as part of a cron command.
空行,开头的空格和 tab 都将被忽略。
注释以 # 开始,被注释的一行将不会被执行。
注意:注释不允许和要执行的命令参杂在一行中。相似的,注释也不能和环境变量参杂在一行中。
在 crontab 中处于激活状态的一行,不是环境变量就是 cron 命令。
环境变量的格式如下:
name = value
等号周围的空格是可选的,value 中的空格是 value 的一部分。 为了保留前导或后置空格,value 中的字符串可以被包括在单引号或双引号里。
一些环境变量是被守护进程 cron 自动创建的。

  • SHELL 被设置为 /bin/sh
  • LOGNAME 和 HOME 被设置为拥有本 crontab 的用户在 /etc/passwd中的值
  • HOME 和 SHELL 可以通过在 crontab 的设置而覆写;但是 LOGNAME 不行。

注意:LOGNAME 变量在 BSD 系统上被称作 USER。
除了 LOGNAME、HOME 和 SHELL,如果一条 crontab 命令要求在执行后发送邮件的话,cron 也会检查变量 MAILTO。
如果 MAILTO 被定义(非空),邮件将被发送至指定的地址。
如果 MAILTO 被定义,但是为空,将不会发送邮件。否则,邮件将被发送到此 crotnab 的所有者。
如果你打算使用 /bin/mail 而非 /usr/lib/sendmail 作为你的邮递程序,这个选项尤其有用。
注意 /bin/mail不提供别名
Note that /bin/mail does not provide aliasing and UUCP usually does not read its mail. If MAILFROM is defined (and non- empty), it is used as the envelope sender address, otherwise, ``root’’ is used.
By default, cron sends a mail using the ‘Content-Type:’ header of ‘text/plain’ with the ‘charset=’ parameter set to the ‘charmap/codeset’ of the locale in which crond(8) is started up, i.e., either the default system locale, if no LC* environment variables are set, or the locale specified by the LC environment variables (see locale(7)). Different character encodings can be used for mailing cron job outputs by setting the CONTENT_TYPE and CONTENT_TRANSFER_ENCODING variables in a crontab to the correct values of the mail headers of those names.
The CRON_TZ variable specifies the time zone specific for the cron table. The user should enter a time according to the specified time zone into the table. The time used for writ‐ ing into a log file is taken from the local time zone, where the daemon is running.
The MLS_LEVEL environment variable provides support for mul‐ tiple per-job SELinux security contexts in the same crontab. By default, cron jobs execute with the default SELinux secu‐ rity context of the user that created the crontab file. When using multiple security levels and roles, this may not be sufficient, because the same user may be running in different roles or in different security levels. For more information about roles and SELinux MLS/MCS, see selinux(8) and the crontab example mentioned later on in this text. You can set the MLS_LEVEL variable to the SELinux security context string specifying the particular SELinux security context in which you want jobs to be run. crond will then set the execution context of those jobs that meet the specifications of the particular security context. For more information, see crontab(1) -s option.
The RANDOM_DELAY variable allows delaying job startups by random amount of minutes with upper limit specified by the variable. The random scaling factor is determined during the cron daemon startup so it remains constant for the whole run time of the daemon.
The format of a cron command is similar to the V7 standard, with a number of upward-compatible extensions. Each line has five time-and-date fields followed by a username (if this is the system crontab file), and followed by a command. Com‐ mands are executed by cron(8) when the ‘minute’, ‘hour’, and ‘month of the year’ fields match the current time, and at least one of the two ‘day’ fields (‘day of month’, or ‘day of week’) match the current time (see “Note” below).
Note that this means that non-existent times, such as the “missing hours” during the daylight savings time conversion, will never match, causing jobs scheduled during the “missing times” not to be run. Similarly, times that occur more than once (again, during the daylight savings time conversion) will cause matching jobs to be run twice.
cron(8) examines cron entries every minute.
时间和日期的取值范围:
field allowed values
——- ———————
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sunday, or use names)
一个字段中可以包含
星号,表示所有。
可以填入范围内的数字。

  • 用连字符 - 分割的两个数字,意味着包含数字内的范围。
    例如:小时字段的 8-11意味着 8、9、10 和 11。
    第一个数字必须小于等于第二个数字。
  • 可以填入列表(数字或范围的集合,其中用逗号分割)
  • 步长值可于范围连用。
    例如:小时字段的 0-23/2 意味着 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22。
  • 星号后也可以使用步长值小时字段 /2 意味着每两小时。
    Names can also be used for the ‘month’ and ‘day of week’ fields. Use the first three letters of the particular day or month (case does not matter). Ranges or lists of names are not allowed.
    If the UID of the owner is 0 (root), the first character of a crontab entry can be “-“ character. This will prevent cron from writing a syslog message about the command being exe‐ cuted.
    The “sixth” field (the rest of the line) specifies the com‐ mand to be run. The entire command portion of the line, up to a newline or a “%” character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cron‐ file. A “%” character in the command, unless escaped with a backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.
    Note: The day of a command’s execution can be specified in the following two fields — ‘day of month’, and ‘day of week’. If both fields are restricted (i.e., do not contain the “
    “ character), the command will be run when either field matches the current time. For example, “30 4 1,15 * 5” would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

EXAMPLE CRON 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 # CRON_TZ=Japan # 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”
Jobs in /etc/cron.d/ The jobs in cron.d and /etc/crontab are system jobs, which are used usually for more than one user, thus, additionally the username is needed. MAILTO on the first line is optional.
EXAMPLE OF A JOB IN /etc/cron.d/job #login as root #create job with preferred editor (e.g. vim) MAILTO=root root touch /tmp/file
SELinux with multi level security (MLS) In a crontab, it is important to specify a security level by crontab -s or specifying the required level on the first line of the crontab. Each level is specified in /etc/selinux/tar‐ geted/seusers. When using crontab in the MLS mode, it is especially important to: - check/change the actual role, - set correct role for directory, which is used for input/output.
EXAMPLE FOR SELINUX MLS # login as root newrole -r sysadm_r mkdir /tmp/SystemHigh chcon -l SystemHigh /tmp/SystemHigh crontab -e # write in crontab file MLS_LEVEL=SystemHigh 0-59
id -Z > /tmp/SystemHigh/crontest
FILES /etc/crontab main system crontab file. /var/spool/cron/ a directory for storing crontabs defined by users. /etc/cron.d/ a directory for storing system crontabs.
SEE ALSO cron(8), crontab(1)
EXTENSIONS These special time specification “nicknames” which replace the 5 initial time and date fields, and are prefixed with the ‘@’ character, are supported:
@reboot : Run once after reboot.
@yearly : Run once a year, ie. “0 0 1 1
“.
@annually : Run once a year, ie. “0 0 1 1 “.
@monthly : Run once a month, ie. “0 0 1
“.
@weekly : Run once a week, ie. “0 0
0”.
@daily : Run once a day, ie. “0 0
“.
@hourly : Run once an hour, ie. “0 “.
CAVEATS crontab files have to be regular files or symlinks to regular files, they must not be executable or writable for anyone else but the owner. This requirement can be overridden by using the -p option on the crond command line. If inotify support is in use, changes in the symlinked crontabs are not automatically noticed by the cron daemon. The cron daemon must receive a SIGHUP signal to reload the crontabs. This is a limitation of the inotify API.
AUTHOR Paul Vixie ⟨vixie@isc.org
cronie 2012-11-22 CRONTAB(5)