Crontab任务定时

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}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酷咪哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值