Centos 7下利用crontab定时执行任务详解
前言
cron服务是Linux的内置服务,但它不会开机自动启动。可以用以下命令启动和停止服务:
/sbin/servicecrondstart /sbin/servicecrondstop /sbin/servicecrondrestart /sbin/servicecrondreload
以上1-4行分别为启动、停止、重启服务和重新加载配置。
要把cron设为在开机的时候自动启动,在/etc/rc.d/rc.local脚本中加入/sbin/servicecrondstart即可
查看当前用户的crontab,输入crontab-l;
编辑crontab,输入crontab-e;
删除crontab,输入crontab-r
添加任务
crontab-e 0*/1***command 0*/2***command
查询任务是否加了:
crontab-l-uroot#查看root用户 0*/1***command 0*/2***command
基本格式:
*****command
分时日月周命令
第1列表示分钟1~59每分钟用*或者*/1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
crontab文件的一些例子:
3021***/usr/local/etc/rc.d/lighttpdrestart
上面的例子表示每晚的21:30重启apache。
4541,10,22**/usr/local/etc/rc.d/lighttpdrestart
上面的例子表示每月1、10、22日的4:45重启apache。
101**6,0/usr/local/etc/rc.d/lighttpdrestart
上面的例子表示每周六、周日的1:10重启apache。
0,3018-23***/usr/local/etc/rc.d/lighttpdrestart
上面的例子表示在每天18:00至23:00之间每隔30分钟重启apache。
023**6/usr/local/etc/rc.d/lighttpdrestart
上面的例子表示每星期六的11:00pm重启apache。
**/1***/usr/local/etc/rc.d/lighttpdrestart
每一小时重启apache
*23-7/1***/usr/local/etc/rc.d/lighttpdrestart
晚上11点到早上7点之间,每隔一小时重启apache
0114*mon-wed/usr/local/etc/rc.d/lighttpdrestart
每月的4号与每周一到周三的11点重启apache
041jan*/usr/local/etc/rc.d/lighttpdrestart
一月一号的4点重启apache
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。