Linux 定时任务调度配置
1. crontab 定时任务执行机制
1.1 crond 服务状态管理与访问权限控制
crontab 命令依赖于 crond 系统服务的支持,该服务通常默认启用并自动启动。
[root@centos2 ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2019-11-13 16:24:18 CST; 2h 24min ago
Main PID: 1267 (crond)
Tasks: 1
CGroup: /system.slice/crond.service
└─1267 /usr/sbin/crond -n
11月 13 16:24:18 centos2 systemd[1]: Started Command Scheduler.
11月 13 16:24:19 centos2 crond[1267]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 17% if used.)
11月 13 16:24:19 centos2 crond[1267]: (CRON) INFO (running with inotify support)
类似于 at 命令,crontab 的用户访问权限由 /etc/cron.allow 和 /etc/cron.deny 文件控制:
- 当存在
/etc/cron.allow文件时,仅允许该文件中列出的用户使用 crontab,此时/etc/cron.deny被忽略 - 仅存在
/etc/cron.deny文件时,该文件中列出的用户被禁止使用 crontab /etc/cron.allow的优先级高于/etc/cron.deny
1.2 用户级别定时任务配置
各用户可通过以下方式管理个人定时任务(需确保用户未被列入 /etc/cron.deny):
[root@centos2 ~]# crontab [选项]
选项:
-e:编辑定时任务
-l:查看定时任务列表
-r:删除全部定时任务(如需删除特定任务,请使用 -e 编辑)
-u 用户名:管理其他用户的定时任务(仅限 root 用户)
创建定时任务:
[root@centos2 ~]# crontab -e
# 进入 vim 编辑器
* * * * * 执行的命令
时间格式说明:
| 字段位置 | 含义 | 取值范围 |
|---|---|---|
| 第1位 | 分钟 | 0-59 |
| 第2位 | 小时 | 0-23 |
| 第3位 | 日期 | 1-31 |
| 第4位 | 月份 | 1-12 |
| 第5位 | 星期 | 0-7(0和7均表示周日) |
特殊符号说明:
| 符号 | 功能说明 |
|---|---|
| * | 匹配任意时间值 |
| , | 指定多个不连续时间点 |
| - | 指定连续时间区间 |
| */n | 设置每隔n个时间单位执行 |
应用示例:
10 2 * * * command # 每天凌晨2:10执行
10 2 * * 7 command # 每周日凌晨2:10执行
10 2 1,15 * * command # 每月1日和15日凌晨2:10执行
10 2 * * 1-6 command # 周一至周六凌晨2:10执行
*/10 2 * * * command # 每天凌晨2点开始,每10分钟执行一次
10 2 1,15 * 1 command # 每月1日和15日且为周一的2:10执行(建议避免同时使用日期和星期)
操作演示:
[root@centos2 ~]# crontab -e
# 编辑模式下添加
*/5 * * * * echo "123" >> /root/test.log
[root@centos2 ~]# crontab -l
*/5 * * * * echo "123" >> /root/test.log
# 查看当前用户的所有定时任务
[root@centos2 ~]# crontab -r
# 删除当前用户全部定时任务
1.3 定时任务配置要点
- 五个时间字段均不可为空,不确定时可用 "*" 表示任意时间
- 最小时间精度为分钟,最大周期为月
- 避免在同一任务中同时使用日期和星期字段以免混淆
- 建议使用绝对路径以避免环境变量相关错误
- 执行 shell 脚本时需注意环境变量继承问题
1.4 系统级别定时任务配置
用户级别的 crontab -e 命令默认以当前用户身份执行任务。对于需要系统身份执行的任务,应编辑 /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
2. anacron 异步定时任务处理
anacron 主要用于解决服务器非持续运行状态下定时任务遗漏的问题。当服务器关机期间有计划任务未执行时,anacron 可在开机后补执行这些任务。
anacron 通过以下周期进行检查:1天、7天、30天,对应日、周、月任务。
时间戳记录文件位于 /var/spool/anacron/ 目录下的 cron.{daily,weekly,monthly} 文件中。
[root@centos2 ~]# anacron [选项] [任务标识]
选项
-s:根据 /etc/anacrontab 设置的延迟时间开始执行
-n:忽略延迟时间立即执行所有任务
-u:仅更新时间戳文件,不执行任务
参数:
任务标识:/etc/anacrontab 中定义的任务名称
主要配置文件 /etc/anacrontab 内容如下:
# /etc/anacrontab: configuration file for anacron
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
RANDOM_DELAY=45
# 最大随机延迟分钟数
START_HOURS_RANGE=3-22
# 执行时间窗口 3:00-22:00
#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
以 daily 任务为例,执行流程如下:
- 读取
/var/spool/anacron/cron.daily中记录的上次执行时间 - 对比当前时间,若间隔超过1天则触发执行
- 执行时间限定在 3:00-22:00 时间窗口内
- 先等待5分钟强制延迟,再随机延迟0-45分钟
- 使用 nice 命令调整优先级,通过 run-parts 脚本执行
/etc/cron.daily目录下所有可执行文件
自定义配置示例:
[root@centos2 ~]# vim /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
RANDOM_DELAY=0
# 取消随机延迟
START_HOURS_RANGE=3-5
# 限定执行时间为 3:00-5:00
#period in days delay in minutes job-identifier command
1 0 cron.daily nice run-parts /etc/cron.daily
7 0 cron.weekly nice run-parts /etc/cron.weekly
@monthly 0 cron.monthly nice run-parts /etc/cron.monthly
# 取消强制延迟
将需要定期执行的脚本放置到 /etc/cron.{daily,weekly,monthly} 对应目录中即可实现周期性执行,即使服务器临时关机也不会遗漏任务。