Linux定时任务
一. at一次性定时任务
查看at是否安装
chkconfig --list | grep atd
启动at服务
service atd start
-
at的访问控制
- /etc/at.allow(白名单)
- /etc/at.deny(黑名单, 对root不起作用)
- 如果两个文件都不存在, 则只有root能使用
-
at[选项]时间
- 选项一般不常用
- 时间:
- HH:MM 02:30
- HH::MM YYYY-MM-DD 20:30 20180112
- HH::MM[am|pm][mouth][day] 02:30pm July 25
- HH::MM[am|pm] + [minutes|hours|days|weeks] now + 5minutes
示例
at now + 2minutes
# 2min中后执行hello.sh脚本
[root@localhost ~]# at now + 2minutes
at> /root/hello.sh
# 按ctrl+d退出保存
at> <EOT>
job 3 at 2018-01-02 22:11
# 查看at
[root@localhost ~]# atq
3 2018-01-02 22:11 a root
# at -c 工作号
[root@localhost ~]# at -c 5
二. 循环定时任务crontab -e
检查定时任务是否开启
service crond status
chkconfig crond on
-
访问控制
crontab是绑定当前登陆用户, 每个用户都有自己的crontab- 当系统中有
/etc/cron.allow
文件时, 只有写入到该文件的用户可以使用crontab
命令 - 当系统中有
/etc/cron.deny
文件时, 写入到该文件的用户不可以使用crontab
命令, 黑名单 -
/etc/cron.allow
优先级高于/etc/cron.deny
- 当系统中有
-
crontab[选项]
- -e: 编辑crontab定时任务
- -l: 查询crontab定时任务
- -r: 删除当前用户所有的crontab任务
crontab格式
* * * * * 命令
项目 | 含义 | 范围 |
---|---|---|
第一个* | 一小时当中第几分钟 | 0-59 |
第二个* | 一天当中第几小时 | 0-23 |
第三个* | 一个月当中的第几天 | 1-31 |
第四个* | 一年当中的第几个月 | 1-12 |
第五个* | 一周当中的星期几 | 0-7(0和7都是星期日) |
项目 | 含义 |
---|---|
* | 代表任意时间 |
, | 代表不连续时间, 如0 8,12,16 * * * 命令 表示在8点0分, 12点0分, 16点0分 都执行一次命令 |
- | 连续时间范围, 如 0 5 * * 1-6 命令 表示周一到周五凌晨五点0分执行命令 |
*/n | 每隔多久执行一次, 如 * */5 * 3 * 命令 表示三月份每天每隔5小时执行一次命令 |
- crontab注意事项
- 六个选项都不能为空, 必须填写, 如果不确定, 使用"*"代表任意时间
- crontab定时任务, 最小有效时间是分钟, 最大是月份, 年和秒不支持
- 在定义时间时, 日期和星期最好不要在一条定时任务中出现, 都是以天为单位的, 容易引起混乱, 如果出现, 比如
0 5 1,2 * 2 命令
表示每个月1号, 二号或者星期2的凌晨五点执行命令
三. 系统定时任务
系统定时任务, 用于执行一些和用户不想关的系统定时任务
- 编辑
/etc/crontab
,
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
# 需要定义用户
* 5 * * * root /root/test/hello.sh
- anacron配置和总结
anacron是用来保证在系统关机的时候错过的定时任务, 可以在系统开机之后再执行,
我们可以需要定时执行的脚本拷贝到一下三个目录中, 就可以按照各自目录的时间点随机时间执行
/etc/cron.daily #该目录下脚本每天执行一次
/etc/cron.weekly # 该目录下脚本每周执行一次(和上次执行时间比较 > 7 day)
/etc/cron.monthly # 该目录下脚本每月执行一次(和上次执行时间比较 > 1 months)
- anacron主配置文件如下:
vi /etc/anacron
...
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
-
anacron工作流程大概为(以/etc/cron.daily为例):
- 开机后检测
/var/spool/anacron/cron.daily
值, 和当前时间作比较, 如果大于一天, 就会准备执行run-parts /etc/cron.daily
下的脚本 - 系统会强制延后五分钟, 再0-45分钟之后执行, 并且只能在3点-22点执行
- nice会设置优先级
- run-part会0-45分钟随机时间运行
/etc/cron.daily
目录下的脚本
- 开机后检测
vi /etc/cron.d/0hourly
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 每隔一分钟运行/etc/cron.hourly下的脚本
01 * * * * root run-parts /etc/cron.hourly
vi /etc/cron.hourly/0anacron
#!/bin/bash
# Skip excecution unless the date has changed from the previous run
# 检测上次执行时间
if test -r /var/spool/anacron/cron.daily; then
day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
exit 0;
fi
# Skip excecution unless AC powered
if test -x /usr/bin/on_ac_power; then
/usr/bin/on_ac_power &> /dev/null
if test $? -eq 1; then
exit 0
fi
fi
// 设置依赖任务, 此处并不能看懂
/usr/sbin/anacron -s