1.(centos)安装crontab服务nginx
yum install crontabs
2.启动、中止、重启、重载服务apache
/sbin/service crond start /sbin/service crond stop /sbin/service crond restart /sbin/service crond reload
3.服务管理centos
查看crontab服务状态:spa
service crond status
手动启动crontab服务:rest
service crond start
编辑crontab定时任务: (若是想取消定时任务,能够在语句前面加上#,或者直接删除该语句)code
crontab -e
查看crontab定时任务: blog
crontab -l
查看crontab服务是否已设置为开机启动,执行命令:crontab
ntsysv
4.添加任务class
基本格式 :定时任务
分 时 日 月 周 命令 第1列表示分钟1~59 每分钟用*或者 */1表示 第2列表示小时1~23(0表示0点) 第3列表示日期1~31 第4列表示月份1~12 第5列标识号星期0~6(0表示星期天) 第6列要运行的命令
30 21 * * * /usr/local/etc/rc.d/lighttpd restart 上面的例子表示每晚的21:30重启apache 45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart 上面的例子表示每个月一、10、22日的4 : 45重启apache 10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart 上面的例子表示每周6、周日的1 : 10重启apache 0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart 上面的例子表示在天天18 : 00至23 : 00之间每隔30分钟重启apache 0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart 上面的例子表示每星期六的11 : 00 pm重启apache。 * */1 * * * /usr/local/etc/rc.d/lighttpd restart 每一小时重启apache * 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart 晚上11点到早上7点之间,每隔一小时重启apache 0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart 每个月的4号与每周一到周三的11点重启apache 0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart 一月一号的4点重启apache
实际应用:
00 23 * * * systemctl stop nginx.service 00 06 * * * systemctl start nginx.service