Linux 的cron.daily 未能执行问题排查

Linux 的cron.daily 未能执行问题排查

一、问题现象

​ 检查发现系统日志message 非常大,未能正常分割,怀疑是不是日志轮询功能出现异常,手动执行日志分割任务

/usr/sbin/logrotate -d /etc/logrotate.d/syslog  # 未发现报错
/usr/sbin/logrotate  -f /etc/logrotate.d/syslog # 发现日志分割正常

在这里插入图片描述

推测并非为logrotate 问题,可能是未执行日志轮转。

二、排查过程

1、确认计划任务执行情况

​ logrotate 依赖于系统自身的计划任务cron.daily,而cron.daily由anacron执行。

[root@Prod-Mana-Kube1 cron.d]# cd /etc/cron.daily/
[root@Prod-Mana-Kube1 cron.daily]# ls
atop.daily  logrotate  man-db.cron  mlocate

​ anacron不是守护进程,可以看做是cron守护进程的某种补充程序,anacron是独立的linux程序,被cron守护进程或者其他开机脚本启动运行,可以每天、每周、每个月周期性地执行一项任务(最小单位为天)。anacron的配置文件为/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

​ 在/etc/anacrontab中的三条默认anacron任务中,nice命令用来调整后面命令的优先级,run-parts命令用来执行设置的目录下的所有脚本,就是说这三条任务分别每天,每周,每月执行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly目录下的脚本。所以我们不用自己往/etc/anacrontab中添加anacrontab任务,而只需把脚本放到相应的目录下,anacron就会周期性的执行这些脚本了。

​ 但是anacron不是守护进程,不会自动执行。在centos7中,cron会运行/etc/cron.d/0hourly,在/etc/cron.d/0hourly

# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly

再看/etc/cron.hourly

[root@Prod-Mana-Kube1 cron.daily]# cd /etc/cron.hourly/
[root@Prod-Mana-Kube1 cron.hourly]# ls
0anacron
[root@Prod-Mana-Kube1 cron.hourly]# cat 0anacron 
#!/bin/sh
# Check whether 0anacron was run today already
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

# Do not run jobs when on battery power
if test -x /usr/bin/on_ac_power; then
    /usr/bin/on_ac_power >/dev/null 2>&1
    if test $? -eq 1; then
    exit 0
    fi
fi
/usr/sbin/anacron -s
# 在这个脚本中会运行anacron

也就是说执行的过程应该是:crond 服务—>/etc/cron.d/0hourly—>/etc/cron.hourly—>/usr/sbin/anacron -s —>/etc/cron.daily

2、检查执行结果

[root@Prod-Mana-Kube1 cron.hourly]#  cd /var/spool/anacron/   # 这个目录中记录了上次anacron 执行成功的时间
[root@Prod-Mana-Kube1 anacron]# ls
cron.daily  cron.monthly  cron.weekly
[root@Prod-Mana-Kube1 anacron]# cat cron.daily 
20211011
[root@Prod-Mana-Kube1 anacron]# cat cron.monthly 
20211116
[root@Prod-Mana-Kube1 anacron]# cat cron.weekly 
20211011

发现cron.daily cron.weekly 确实已经很久没有执行了,且上次执行成功时间,与日志生成时间一致,推测问题确实出在这里。

在这里插入图片描述

手动执行run-parts /etc/cron.hourly ,发现进程卡主,无法执行成功。(之前检查/var/log/cron 文件,日志中报错Job `cron.daily’ locked by another anacron - skipping ,但是未能找到报这个错的原因),检查run-parts 进程,发现存在一个之前的进程,与故障时间一致,推测可能与这个进程残留有关,关闭这个进程,再次查看计划任务执行情况

在这里插入图片描述

发现已经自动开始分割(anacron在发现本周期内未执行成功时,会自动立刻执行)

在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值