Linux之crontab定时任务
一、安装
1.检测系统是否安装crond服务
service crond status
2.开始安装
yum -y install vixie-cron
yum -y install crontabs
vixie-cron 软件包是 cron 的主程序;
crontabs 软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。
设置开机自启
chkconfig --level 345 crond on
3.直接输入命令crontab -e 或者编辑文件/etc/crontab 就可以直接设置定时任务。
crontab -e
或者 vi /etc/crontab
二、crontab 常用服务命令
1、/sbin/service crond start 启动服务
2、/sbin/service crond stop 关闭服务
3、/sbin/service crond restart 重启服务
4、/sbin/service crond reload 重新载入配置
5、/sbin/service crond status 服务是否运行
6、crontab -l 列出某个用户的任务计划
实例
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。
* */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
*/30 * * * * /usr/sbin/ntpdate 210.72.145.44 //每半小时同步一下时间