Linux中,大部分系统管理工作都是通过定期自动执行某一个脚本来完成的。cron就用于设置周期性被执行的指令。
一、安装(系统默认安装。不过在有些时候会失效,已知有:卸载postfix和编译安装lnmp环境)
# yum -y install vixie-cron
# yum -y install crontabs
二、配置 service crond stop //关闭服务
service crond restart //重启服务
service crond status //查看服务状态
加入开机自动启动: # chkconfig --add crond
# chkconfig crond on
三、crontab 命令 编写任务计划
# crontab -e
删除计划任务
# crontab -r
查看任务计划
# crontab -l
-u<用户名称> :指定某个用户,不加-u选项则为当前用户。四、格式:
#crontab -e
* * * * * command
分 时 日 月 周 命令第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
例子:
30 21 * * * /usr/local/sbin/httpd restart 每晚的21:30重启apache。 45 4 1,10,22 * * /usr/local/sbin/httpd restart 每月1、10、22日的4 : 45重启apache。 10 1 * * 6,0 /usr/local/sbin/httpd restart 每周六、周日的1 : 10重启apache。 0,30 18-23 * * * /usr/local/sbin/httpd restart 每天18 : 00至23 : 00之间每隔30分钟重启apache。 0 23 * * 6 /usr/local/sbin/httpd restart 每星期六的11 : 00 pm重启apache。 * */1 * * * /usr/local/sbin/httpd restart 每一小时重启apache * 23-7/1 * * * /usr/local/sbin/httpd restart 11点到早上7点之间,每隔一小时重启apache 0 11 4 * mon-wed /usr/local/sbin/httpd restart 每月的4号与每周一到周三的11点重启apache 0 4 1 jan * /usr/localsbin/httpd restart 一月一号的4点重启apache */30 * * * * /usr/sbin/ntpdate 210.72.145.44 每半小时同步一下时间
我来说说