centos # 重启服务 service crond restart ----------------------------------------- chkconfig crond on systemctl list-unit-files crond on # 查询,提示被覆盖 chkconfig --list | grep cron ------------------------------------------ systemctl list-unit-files | grep cron crond.service enabled ------------------------------------------
vi /etc/cron. cron.d/ cron.daily/ cron.deny cron.hourly/ cron.monthly/ cron.weekly/
crontab 【选项】 -e: 编辑crontab 定时任务 -l: 查询crontab 任务 -r:删除当前用户全部crontab任务 注意: crontab 是当前用户的,只有当前用户的权限,crontab 会绑定 user 的身份
crontab -e 会进入一个当前用户的文件,在这个文件下编写任务 编写任务格式 """ * * * * * 执行任务 第一个* 一小时当中的第几分钟0-59 第二个* 一天当中的第几个小时 0-23 第三个* 一月当中的第几天 1-13 第四个* 一年当中的第几个月 1-12 第五个* 一周当中的第几个星期 0-7 0,7都表明周日 """ 例如 *********************************** 10 * * * * /root/nginx_start.sh 会在1点10分,2点10,一小时执行一次 ************************************ ************************************ 若是想每隔10分钟执行一次 */10 * * * * * /pwd/脚本 *表明任意时间,每隔十分钟执行一次 ************************************ +++++++++++++++++++++++++++++++++++++++ * * * * * 这个是每分钟执行一次 ,表明不连续时间 0 8,12,16 * * * 天天8点整 12点整 16点整 都执行一次 +++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++ -表明连续的时间 0 5 * * 1-6 周1到周6的早上5点整执行 +++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++ 45 22 * * * 天天22点45分 0 17 * * 1 每周1的17点整 0 5 1,15 * * 每个月1号和5号 的 5点整 40 4 * * 1-5 周1到周5的4点40分 */10 4 * * * 4点 4点10分 4点20分 ...4点50分 1小时执行6次 0 0 1,5 * 1 每周1 或1号 或 5号 的凌晨 +++++++++++++++++++++++++++++++++++++++ """ 注意事项 星期和几号 不要同时出现,本身容易混乱 最小时间范围是分钟 最大时间范围是月 超出范围都不能实现 六个选项不能为空 脚本执行尽可能使用绝对路径 系统的path环境变量,和咱们用户的环境变量并不彻底一致 ""'
crontab -e 是把定时任务绑定到当前用户上的 # 使用配置文件编写定时任务 /etc/crontab 这个是配置文件
vi /etc/crontab #################################### SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # 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 user-name 指定执行用户名 command to be executed 执行的命令或者脚本 ******************************************************** 第二种方法 将脚本放入 /etc/cron.{daily,weekly,monthly} 目录下,会自动天天 每周 每个月 执行一次 ********************************************************
""" 定时任务 尽可能把时间错开 """
这个任务的好处是 若是我定在5点执行一个任务 可是我4点50-5点10分关机了 当我5点10分开机后,anacron 会检测有没有漏掉的定时任务,会从新执行 它只会检测目录下的执行任务 ,crontab -e 的不会检测 anacron 检测周期 vi /var/spool/anacron/ 目录记录的是上一次执行时间 下一次执行 会和上一次进行比较 anacron 检测时间比较粗略 vi /etc/anacrontab “”“ # /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # 随机延迟时间,错峰执行 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly ”“”
来源:https://blog.csdn.net/sunt2018/article/details/86499396nginx