crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
选件
file
|
从指定文件加载crontab数据。如果file为破折号(“ - ”),则从标准输入读取crontab数据。
|
-u user
|
指定要查看或修改其crontab的用户。如果未提供此选项,则crontab将打开运行crontab的用户的crontab。注意:使用su切换用户可能会混淆crontab,因此,如果在su内部运行它,请始终使用-u选项以避免产生歧义。
|
-l
|
显示当前的crontab。
|
-r
|
删除当前的crontab。
|
-e
|
使用环境变量VISUAL或EDITOR中指定的编辑器编辑当前crontab 。
|
-i
|
与-r相同,但是在删除crontab之前会向用户提供是/否确认提示。
|
-s
|
仅限SELinux:在进行编辑或替换之前,将当前SELinux安全上下文字符串作为MLS_LEVEL设置附加到crontab文件中。有关详细信息,请参见SELinux文档。
|
总览
所述的crontab命令用于查看或编辑指令的表,以通过运行cron的。
系统上的每个用户都可以拥有一个个人crontab。
Crontab文件位于/ var / spool /(或子目录,如/ var / spool / cron / crontabs)中,但不能直接对其进行编辑。而是通过运行crontab对其进行编辑。
Cron命令条目
crontab文件中的每个cron命令条目都有五个时间和日期字段(紧随其后的是用户名,仅当它是系统crontab文件时),然后是命令。
当分钟,小时和月字段与当前时间匹配,并且两天字段中的至少一个(月日或星期几)与当前日期匹配时,命令由cron执行。
cron守护进程检查crontab中每分钟一次。
注意
无用时间,例如夏令时“春季向前”期间的“缺勤时间”,将永远不会匹配。这将导致在“丢失时间”内计划的作业在这些时间段内无法运行。出于同样的原因,夏令时(秋季)中多次发生的时间将导致匹配的作业运行两次。
时间和日期字段
领域
|
允许值
|
分钟
|
0-59
|
小时
|
0-23
|
一个月中的某天
|
1-31
|
月
|
1-12 (或名称;见下例)
|
星期几
|
0-7 (0或7是星期日,或使用的名称;见下文)
|
这些字段中的任何一个都可以设置为星号(*),代表“第一到最后”。例如,要每月运行一份工作,请在“ 月”字段中输入*。
允许使用数字范围。范围是用连字符分隔的两个数字。指定的范围包括端值;例如,“小时”条目的8-11指定在8、9、10和11小时执行。
列表是允许的。列表是一组用逗号分隔的数字(或范围)。例如:“ 1,2,5,9 ”,“ 0-4,8-12 ”。
步长值可以与范围结合使用。例如,“ 小时”字段中的“ 0-23 / 2 ” 表示“每隔一小时”。星号后还允许执行步骤,因此,如果您想说“每两个小时”,则可以使用“ * / 2 ”。
名称也可以用于“月”和“星期几”字段。使用特定日期或月份的前三个字母(大小写无关)。不允许使用名称的范围或列表。
“第六个”字段(该行的其余部分)指定要运行的命令。该行的整个命令部分,直到换行符或% 字符,将由/ bin / sh或cronfile的SHELL变量中指定的shell执行。除非使用反斜杠(\)进行转义,否则命令中的百分号(%)将被更改为换行符,并且第一个%之后的所有数据将作为标准输入发送到命令。
请注意,命令的执行日期可以由两个字段指定:月份的日期和星期几。如果两个字段都受限制(换句话说,它们不是*),则当任何一个字段与当前时间匹配时,将运行该命令。例如,“ 30 4 1,15 * 5 ”将导致一个命令在每月的1号和15号以及每个星期五的4:30 AM运行。
crontab文件
crontab文件的每一行都是“活动”或“非活动”。“活动”行是环境设置或cron命令条目。“无效”行将被忽略,包括注释。
空行以及前导空格和制表符将被忽略。第一个非空格字符为井号(#)的行被解释为注释,并被忽略。注释与cron命令不在同一行,因为它们将被解释为命令的一部分。出于相同的原因,注释不能与环境变量设置放在同一行。
环境设定
crontab中的环境设置行可以在cron运行作业时设置环境变量。
提示
并非每个系统的crontab都可以包含环境设置。在Ubuntu和Debian以及使用GNU mcron的系统上,可以在crontab中进行环境设置。在其他系统上,例如Arch Linux和Fedora,不允许在crontab中进行环境设置。查看您的发行版的cron文档以获取更多信息。
crontab中的环境设置的格式为:
name = value
围绕等号(空格=)是可选的,并且在任何后续非前导空格值将被分配给值的一部分名称。可以将值字符串放在引号中(单引号或双引号,但匹配),以保留前导或尾随的空格。
一些环境变量是由cron自动设置的:
-
SHELL设置为/ bin / sh。
-
LOGNAME和HOME是在crontab所有者的/ etc / passwd行中设置的。HOME和SHELL可能会在运行时被crontab中的设置覆盖;LOGNAME可能不会。
-
在BSD系统上,LOGNAME变量有时称为USER。在这些系统上,还将设置USER。
组态
允许用户运行Cron作业
如文件/etc/cron.allow和/etc/cron.deny所定义,可以为单个用户允许或禁止Cron作业。如果cron.allow存在,则必须在其中列出用户才能使用给定命令。如果cron.allow文件不存在,但cron.deny文件存在,则不得在此处列出用户使用给定命令。如果这两个文件都不存在,则仅超级用户可以使用给定命令。
也可以使用PAM(可插入身份验证模块)身份验证来定义Cron权限,以设置可能使用或不使用crontab和系统cron作业的用户。PAM配置位于/etc/cron.d/中。
配置临时目录
可以在下面列出的环境变量中设置cron作业的临时目录。如果未定义这些变量,则使用默认的临时目录/ tmp。
配置文件
文件
|
描述
|
/etc/cron.allow
|
如果存在此文件,则必须在此文件中列出用户才能运行cron作业。
|
/etc/cron.deny
|
如果该文件存在,用户必须不被此文件中列出要能够运行cron作业。
|
如果两个配置文件都不存在,则只有超级用户可以运行cron作业。
crontab [-u user] file
crontab [-u user] [-l | -r | -e] [-i] [-s]
Options
file
|
Load the crontab data from the specified file. If file is a dash ("-"), the crontab data is read from standard input.
|
-u user
|
Specifies the user whose crontab is to be viewed or modified. If this option is not given, crontab opens the crontab of the user who ran crontab. Note: using su to switch users can confuse crontab, so if you are running it inside of su, always use the -u option to avoid ambiguity.
|
-l
|
Display the current crontab.
|
-r
|
Remove the current crontab.
|
-e
|
Edit the current crontab, using the editor specified in the environment variable VISUAL or EDITOR.
|
-i
|
Same as -r, but gives the user a yes/no confirmation prompt before removing the crontab.
|
-s
|
SELinux only: appends the current SELinux security context string as an MLS_LEVEL setting to the crontab file before editing or replacement occurs. See your SELinux documentation for detailed information.
|
Overview
The crontab command is used to view or edit the table of commands to be run by cron.
Each user on your system can have a personal crontab.
Crontab files are located in /var/spool/ (or a subdirectory such as /var/spool/cron/crontabs), but they are not intended to be edited directly. Instead, they are edited by running crontab.
Cron command entries
Each cron command entry in the crontab file has five time and date fields (followed by a username, only if it is the system crontab file), followed by a command.
Commands are executed by cron when the minute, hour, and month fields match the current time, and at least one of the two day fields (day of month, or day of week) match the current day.
The cron daemon checks the crontab once every minute.
Note
Nonexistent times, such as "missing hours" during daylight savings "Spring forward" days, will never match. This causes jobs scheduled during the "missing times" not to run during those times. For the same reason, times that occur more than once during daylight savings (in the autumn) will cause matching jobs to run twice.
Time and date fields
field
|
allowed values
|
minute
|
0-59
|
hour
|
0-23
|
day of month
|
1-31
|
month
|
1-12 (or names; see example below)
|
day of week
|
0-7 (0 or 7 is Sunday, or use names; see below)
|
Any of these fields can be set to an asterisk (*), which stands for "first through last". For instance, to run a job every month, put * in the Month field.
Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive; for example, 8-11 for an "hours" entry specifies execution at hours 8, 9, 10 and 11.
Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: "1,2,5,9", "0-4,8-12".
Step values can be used in conjunction with ranges. For example, "0-23/2" in the Hours field means "every other hour." Steps are also permitted after an asterisk, so if you want to say "every two hours", you can use "*/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 doesn't matter). Ranges or lists of names are not allowed.
The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. 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.
Note that the day of a command's execution can be specified by two fields: day of month, and day of week. If both fields are restricted (in other words, they aren't *), 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 A.M. on the 1st and 15th of each month, plus every Friday.
The crontab file
Each line of a crontab file is either "active" or "inactive". An "active" line is an environment setting, or a cron command entry. An "inactive" line is anything ignored, including comments.
Blank lines and leading spaces and tabs are ignored. Lines whose first non-space character is a pound sign (#) are interpreted as comments, and are ignored. Comments are not allowed on the same line as cron commands, because they will be interpreted as part of the command. For the same reason, comments are not allowed on the same line as environment variable settings.
Environment settings
An environment setting line in the crontab can set environment variablesfor whenever cron runs a job.
Tip
Not every system's crontab can include environment settings. On Ubuntu and Debian, and systems that use GNU mcron, environment settings can be made in the crontab. On other systems, such as Arch Linux and Fedora, environment settings in the crontab are not allowed. Check your distribution's cron documentation for more information.
An environment setting in the crontab is formatted as:
name = value
The spaces around the equal sign (=) are optional, and any subsequent non-leading spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing blanks.
Some environment variables are set automatically by cron:
-
SHELL is set to /bin/sh.
-
LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner. HOME and SHELL may be overridden at runtime by settings in the crontab; LOGNAME may not.
-
The LOGNAME variable is sometimes called USER on BSD systems. On these systems, USER will be set also.
Configuration
Permitting users to run cron jobs
Cron jobs can be allowed or disallowed for individual users, as defined in the files /etc/cron.allow and /etc/cron.deny. If cron.allow exists, a user must be listed there to be allowed to use a given command. If the cron.allow file does not exist but the cron.deny file does, then a user must not be listed there to use a given command. If neither of these files exists, only the superuser will be allowed to use a given command.
Cron permissions can also be defined using PAM (pluggable authentication module) authentication to set up users who may or may not use crontab and system cron jobs. PAM configuration is located in /etc/cron.d/.
Configuring the temp directory
The temporary directory for cron jobs can be set in environment variables listed below. If these variables are not defined, the default temporary directory /tmp is used.
Configuration files
File
|
Description
|
/etc/cron.allow
|
If this file exists, users must be listed in this file to be able to run cron jobs.
|
/etc/cron.deny
|
If this file exists, users must not be listed in this file to be able to run cron jobs.
|
If neither configuration file exists, only the superuser may run cron jobs.