|
4 | 4 |
|
5 | 5 | > **cron** is the general name for the service that runs scheduled actions. **crond** is the name of the daemon that runs in the background and reads **crontab** files.
|
6 | 6 |
|
7 |
| -简单理解:cron 是服务,crond 是收据进程, crontab 的 crond 的配置文件。 |
| 7 | +简单理解:cron 是服务,crond 是守护进程, crontab 的 crond 的配置文件。 |
8 | 8 |
|
9 | 9 | ### 1.2 crontab 选项
|
10 | 10 |
|
11 |
| -+ `crontab -e` : Edit your crontab file, or create one if it doesn't already exist. |
| 11 | ++ `crontab -e` : Edit your crontab file, or create one if it doesn't already exist. # 推荐使用命令新增计划任务--语法检查 |
12 | 12 | + `crontab -l` : Display your crontab file.
|
13 |
| -+ `crontab -r` : Remove your crontab file. |
| 13 | ++ `crontab -r` : Remove your crontab file. # 慎用 |
14 | 14 | + `crontab -u user` : Used in conjunction with other options, this option allows you to modify or view the crontab file of user. When available, only administrators can use this option.
|
15 | 15 |
|
16 | 16 | ### 1.3 crontab 格式
|
17 | 17 |
|
18 | 18 | minute(s) hour(s) day(s) month(s) weekday(s) command(s)
|
19 | 19 |
|
20 |
| -+ minute: 0-59 |
21 |
| -+ hour: 0-23 |
22 |
| -+ day: 1-31 |
23 |
| -+ month: 1-12 |
24 |
| -+ weekday: 0-6, 周日:0, 周一:1, 以此类推 |
25 |
| -+ command: 执行命令(注意要写绝对路径) |
| 20 | +``` |
| 21 | +# Use the hash sign to prefix a comment |
| 22 | +# +—————- minute (0 – 59) |
| 23 | +# | +————- hour (0 – 23) |
| 24 | +# | | +———- day of month (1 – 31) |
| 25 | +# | | | +——- month (1 – 12) |
| 26 | +# | | | | +—- day of week (0 – 7) (Sunday=0 or 7) |
| 27 | +# | | | | | |
| 28 | +# * * * * * command to be executed |
| 29 | +``` |
26 | 30 |
|
27 | 31 | ## 二、使用举例
|
28 | 32 |
|
|
41 | 45 |
|
42 | 46 | 0 10 * * * 5 /home/jerryzhang/update_weekly.py
|
43 | 47 |
|
| 48 | +(4) 每天 6, 12, 18 点执行一次命令 |
| 49 | + |
| 50 | + 0 6,12,18 * * * /bin/echo hello |
| 51 | + |
| 52 | +(5) 每天 13 点到 17 点执行一次命令 |
| 53 | + |
| 54 | + 0 13-17 * * * /bin/echo hello |
| 55 | + |
| 56 | +__注:__ |
| 57 | + |
| 58 | +* 程序执行完毕,系统会给对应用户发送邮件,显示该程序执行内容,如果不想收到,可以重定向内容 `> /dev/null 2>&1` |
| 59 | +* 如果执行语句中有 `%` 号,需要使用反斜杠 '\' 转义 |
| 60 | + |
44 | 61 | ## 三、参考资料
|
45 | 62 |
|
46 | 63 | + [维基百科: Cron](http://zh.wikipedia.org/wiki/Cron)
|
|
0 commit comments