循环执行的例行命令:crontab
crontab的启动、重启、停止、状态查询
service crond start
service crond restart
service crond stop
service crond status
crontab [-u user] [-l | -e | -r]
参数说明:
-u user:只有在root下才可以使用,直接查看或编译其他使用者的crontab的内容
-l :列出当前用户的crontab内容
-e :编译当前用户的crontab内容
-r :删除当前用户的crontab内容,如果当前用户下有多个定时命令,不建议使用
e:每分钟向指定文件追加信息,输入crontab -e进入编译模式
*/1 * * * * echo "my crontab test" >> /root/TIMESERV/log/test.txt
上述命令的基本格式:
* * * * * command
分 时 日 月 周 命令
数字代表的意义 分钟 小时 日期 月份 周
范围 0-59 0-23 1-31 1-12 0-6 (0为星期天)
* 则代表通配符
*/n 则代表每隔多久
再举一些例子(此处引用了他人的例子,哈哈):
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每晚的21:30重启apache。
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每月1、10、22日的4 : 45重启apache。
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
上面的例子表示每周六、周日的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。
0 /1 * * /usr/local/etc/rc.d/lighttpd restart
每一小时重启apache
0 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
如果定时命令出比较多和复杂,也可以使用shell脚本写好命令,再编辑到crontab内容中
如现在已经编写好一个shell脚本:/home/xxx/myshell/test.sh
执行:
1、chmod +x /home/xxx/myshell/test.sh 设置为可执行文件
2、crontab -e进入编译,将脚本追加到crontab内容中
* * * * * /home/xxx/myshell/test.sh
最后,crontab执行日志的查看方式:
view /var/log/cron