操作系统
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
# 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
50 14 * * * root /bin/sh /root/root.sh #表示每天14:50分执行root.sh这个脚本
$ systemctl reload crond.service #重新载入配置
$ tail -f /var/log/cron #查看任务日志
格式说明
[minute] [hour] [day] [mounth] [week] [user-name] [command]
- minute:分,值为0--59
- hour:时,值为1--23
- day:天,值为1--31
- month:月,值为1--12
- weekday:星期,值为0--6 【0代表星期日,1代表星期一,依次类推】
- user-name: 执行程序的用户名
- command:要执行的程序路径【绝对路径】