1.背景
ubuntu 16.04 麒麟桌面版
ubuntu 16.04 server
2.介绍
man crontab | more查看详细介绍
官方文档CronHowto
3.实施
3.1.crontab文本
打开文本/etc/crontab,内容如下
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
3.2.crontab文本格式
添加的指令格式:m h dom mon dow user command,依次为分(059)、时(023)、日期(031)、月(012)、星期(0~6,周末用0表示)、用户、预执行指令
分钟 0-59
小时 0-23
天 1-31
月 1-12
星期 0-6,0表示星期天
除了这些固定值外,还可以配合星号(*),逗号(,),和斜线(/)来表示一些其他的含义:
* 表示任意值,比如在小时部分填写 * 代表任意小时(每小时)
, 可以允许在一个部分中填写多个值,比如在分钟部分填写 1,3 表示一分钟或三分钟
/ 一般配合 * 使用,代表每隔多长时间,比如在小时部分填写 */2 代表每隔两分钟。所以 */1 和 * 没有区别,*/2 可以看成是能被2整除的任意值。
以下是一些例子(省略了命令部分):
* * * * * # 每隔一分钟执行一次任务
0 * * * * # 每小时的0点执行一次任务,比如6:00,10:00
6,10 * 2 * * # 每个月2号,每小时的6分和10分执行一次任务
*/3,*/5 * * * * # 每隔3分钟或5分钟执行一次任务,比如10:03,10:05,10:06
3.3.注意
添加完成后记得重启服务器或者重启服务(service cron restart)。
4.指令
Usage: /etc/init.d/cron {start|stop|status|restart|reload|force-reload}
service cron {start|stop|status|restart|reload|force-reload}