crontab 是一个用于设置周期性被执行的任务的工具
安装
yum install vixie-cron
yum install crontabs
启动
service crond start
检查任务列表
crontab -l
编辑任务
crontab -e
说明:和vi操作类似
案例
crontab -e
*/1 * * * * date >> /tmp/log.txt
tail -10f /tmp/log.txt
基本组成
crond 系统服务 从配置文件中刷新定时任务
常用操作:
service crond start
service crond stop
service crond status
配置文件 配置定时任务
如:/etc/crontab
/etc/cront.d/*
/etc/cront.d/sysstat
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
crontab 配置工具 调整定时任务
日志
/var/log/cron*
定时任务案例
每天2:30重启apache
30 2 * * * service httpd restart
每个月的1号10号20号的1:30
30 1 1,10,20 * * service httpd restart
每个月的1号到10号的1:30
30 1 1-10 * * service httpd restart
每个月的1号到10号和20号到23号的1:30
30 1 1-10,20-23 * * service httpd restart
每隔2分钟
*/2 * * * * service httpd restart
1-59/2 * * * * service httpd restart
(0、2、4、6···)
(1、3、5、7···)
晚上23点到7点之间每隔1小时
0 23-7/1 * * * service httpd restart
每天18点到23点每隔30分钟
0,30 18-23 * * * service httpd restart
0-59/30 18-23 * * * service httpd restart
总结:
* 任意时间
A,B,C A 或 B或 C
A-B A 到 B
*/A 每隔A时间