一、安装
yum -y install vixie-cron
yum -y install crontabs
说明:
vixie-cron 软件包是 cron 的主程序;
crontabs 软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。
二、配置
可以用以下的方法启动、关闭这个服务:
systemclt start crond //启动服务
systemclt stop crond //关闭服务
systemclt restart crond//重启服务
systemctl status crond //查看crontab服务状态
三、Crontab文件的格式:
minute:分,值为0-59
hour:小时,值为1-23
day:天,值为1-31
month:月,值为1-12
weekday:星期,值为0-6(0代表星期天,1代表星期一,以此类推)
username:要执行程序的用户,一般设置为root
command:要执行的程序路径(设置为绝对路径)例如:/home/yusi123.com/yusi123.sh
root身份登录到命令行
输入crontab -e
按下a键进入到编辑模式
输入 执行代码,例如:
00 00 * * * /bin/bash /data/sh_file/auto_cut_nginx_log.sh //每天0点自动切割日志
附:crontab规则详细实例
1、每天6:00执行
0 6 * * * root /data/sh_file/auto_cut_nginx_log.sh
2、每周六凌晨4:00执行
0 4 * * 6 root /data/sh_file/auto_cut_nginx_log.sh
3、每周六凌晨4:25执行
25 4 * * 6 /data/sh_file/auto_cut_nginx_log.sh
4、每周一到周五的11:41开始,每隔10分钟执行一次
41,51 11 * * 1-5 root /data/sh_file/auto_cut_nginx_log.sh
1-59/10 12-23 * * 1-5 root /data/sh_file/auto_cut_nginx_log.sh
5、在每天的10:31开始,每隔2小时重复一次
31 10-23/2 * * * root /data/sh_file/auto_cut_nginx_log.sh
6、每天15:00执行
0 15 * * * root /data/sh_file/auto_cut_nginx_log.sh