操作系统
CentOS7
安装
$ yum install crontabs
$ systemctl status crond.service #查看crontab服务状态
$ systemctl start crond.service #启动服务
$ systemctl restart crond.service #重启服务
$ systemctl reload crond.service #重新载入配置
使用
举例: /root/root.sh 要自动定时执行的脚本程序路径
$ chmod +x /root/root.sh #对脚本文件添加执行权限,否则不能执行
$ vi /etc/crontab #编辑配置文件
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
MAILTO=root
50 14 * * * /bin/sh /root/root.sh #表示每天14:50分执行root.sh这个脚本
$ systemctl reload crond.service #重新载入配置
$ tail -f /var/log/cron #查看任务日志
格式说明
[minute] [hour] [day] [mounth] [week] [day] [command]
- minute:分,值为0--59
- hour:时,值为1--23
- day:天,值为1--31
- month:月,值为1--12
- weekday:星期,值为0--6 【0代表星期日,1代表星期一,一次类推】
- command:要执行的程序路径【绝对路径】
更多实例
1.每天6:00执行
0 6 * * * /bin/sh /root/root.sh
2.每周六凌晨4:00执行
0 4 * * 6 /bin/sh /root/root.sh
3.每周六凌晨4:05执行
5 4 * * 6 /bin/sh /root/root.sh
4.每周六凌晨4:15执行
15 4 * * 6 /bin/sh /root/root.sh
5.每周六凌晨4:25执行
25 4 * * 6 /bin/sh /root/root.sh
6.每周一到周五的11:41开始,每10分钟执行一次
1-59/10 12-23 * * 1-5 /bin/sh /root/root.sh
7.每天的10:31开始,没隔2小时执行一次
31 10-23/2 * * * /bin/sh /root/root.sh