Crontab - Periodic Tasks
cron is a periodic-task tool found in Unix-like systems [1], letting system maintenance and administration be automated. The name cron comes from the ancient Greek word for “time”, χρόνος (chronos).
Configuration
cron’s configuration file is called crontab, short for “cron table”. It’s split into system crontabs and user crontabs
System crontab
The crontab file for system maintenance and other tasks defined by the system administrator; its format differs slightly from a user task configuration file, and you can specify which user identity runs a task via the USERNAME field.
cat /etc/crontab
SHELL=/bin/sh 指定要使用的shell
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 指定了系统执行命令的路径
MAILTO=USERNAME 通过电子邮件发送任务执行信息给用户
HOME=/ 执行任务时使用的主目录
* * * * * USERNAME 命令
User crontab
User crontab files are stored under /var/spool/cron/[crontabs], named after the owning user’s login name; cron uses the file name to decide which user identity to run the task as.
cat /var/spool/cron/[crontabs] USERNAME
* * * * * 命令
Crontab format
* * * * * [USERNAME] 命令
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └────── 星期 (0 - 6) (0 至 6 是周日至周六)
│ │ │ └────── 月份 (1 - 12)
│ │ └────── 日期 (1 - 31)
│ └────── 小时 (0 - 23)
└────── 分钟 (0 - 59)
cron reads the crontab file once every minute and runs the specified tasks according to the times given in the crontab file.
Each line of a crontab file represents a task, following a specific format, separated by spaces or tabs.
The first 5 time fields can use special characters
Asterisk (*) matches any possible value
Hyphen (-) separates two values, setting a range
Comma (,) separates integers or ranges, matching all the listed values
Slash (/) sets an interval
Day-of-week and day-of-month have a potential ambiguity — if both are specified, the task runs whenever either condition is satisfied.
The *USERNAME* field only appears in the system crontab file /etc/crontab and files under the /etc/cron.d directory, and is used to specify which user identity runs the task. This field neither appears in, nor is needed in, user crontab files, because the user crontab’s file name already implies the UID
The command can be either a system command or a written script
Syntax
crontab [OPTION] FILE
crontab [OPTION]
Load FILE as the crontab task list file into crontab, replacing all previous versions
-u user: sets the crontab service for a given user
-e: edit a given user’s crontab file contents. If no user is specified, this edits the current user’s crontab file
-l: show a given user’s crontab file contents. If no user is specified, this shows the current user’s crontab file contents
-r: delete a given user’s crontab file. If no user is specified, this deletes the current user’s crontab file by default
-i: prompt for confirmation when deleting a user’s crontab file
Permissions
/etc/cron.deny and /etc/cron.allow specify which users may submit crontab files. If a cron.allow file exists, it contains the full list of users allowed to submit a crontab, one user per line, and any user not listed cannot use crontab. If no cron.allow file exists, cron checks whether a cron.deny file exists; besides the users listed there, all other users are allowed to use crontab. If neither cron.allow nor cron.deny exists, then only the root user can use crontab (Debian allows all users to use crontab).
Examples
crontab -u root -e 编辑root用户的crontab文件
* * * * * command 每1分钟执行一次 [#]_
3,15 * * * * command 每小时的第3和第15分钟执行
3,15 8-11 * * * command 在8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * command 每隔两天的8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 command 每个星期一的8点到11点的第3和第15分钟执行
30 21 * * * command 每晚的21:30执行
45 4 1,10,22 * * command 每月1、10、22日的4:45执行
10 1 * * 6,0 command 每周六、周日的1:10执行
0,30 18-23 * * * command 每天18点至23点之间每隔30分钟执行
0 23 * * 6 command 每个星期六的23点执行
* */1 * * * command 每一小时执行
* 23-7/1 * * * commadn 晚上23点到早上7点之间,每隔一小时执行
When you save the file, cron checks it; if any field has a value outside the allowed range, cron issues a warning
References