logrotate

logrotate介绍

logrotate是基于crond服务来运行的,其crond服务的脚本是/etc/cron.daily/logrotate,日志转储是系统自动完成的。实际运行时,logrotate会调用主配置文件 /etc/logrotate.conf,可以在 /etc/logrotate.d 目录里放置自定义好的配置文件,用来覆盖logrotate的缺省值。定时执行/etc/cron.daily目录下的文件的设置,则在/etc/anacrontab里定义的,那anacrontab怎么知道上次成功执行脚本的具体时间呢?通过查看/etc/cron.daily/logrotate得知有 /var/lib/logrotate/logrotate.status这样的一个文件,此文件的作用就是记录最近一次成功运行日志分割脚本的具体时间.
所以logrotate执行脚本的命令其实是/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf。

logrotate执行流程

crond服务加载/etc/cron.d/0hourly ---> 在每小时的01分执行/etc/cron.hourly/0anacron ---> 执行anacron ---> 根据/var/spool/anacron/cron.daily获得上次运行anacron的时间 ---> 根据/etc/anacrontab的配置执行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly ---> 执行/etc/cron.daily/下的logrotate脚本 ---> 执行logrotate ---> 根据/etc/logrotate.conf配置执行脚本/etc/logrotate.d/nginx ---> 转储nginx日志成功

  1. 每小时01分执行/etc/cron.hourly/0anacron
[root@mongodb-1 mongodb]# cat /etc/cron.hourly/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
  1. /var/spool/anacron/cron.daily记录了上次执行logrotate的time_id
[root@mongodb-1 mongodb]# cat /var/spool/anacron/cron.daily
20201123
  1. 校验通过,执行anacron,检查/etc/anacrontab
[root@mongodb-1 mongodb]# cat /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
RANDOM_DELAY=8
# the jobs will be started during the following hours only
#START_HOURS_RANGE=3-22
START_HOURS_RANGE=15-16

#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
  1. 校验通过,执行/etc/cron.daily/logrotate
[root@mongodb-1 mongodb]# cat /etc/cron.daily/logrotate
#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
  1. 检查/var/lib/logrotate/logrotate.status 也记录了上次执行的时间
[root@mongodb-1 mongodb]# cat /var/lib/logrotate/logrotate.status
logrotate state -- version 2
"/var/log/mongodb/mongod.log" 2020-11-23-19:9:44
"/var/log/yum.log" 2020-6-17-8:34:9
"/var/log/boot.log" 2020-11-19-5:45:18

/etc/logrotate.conf总配置
/etc/logrotate.d/xxxx.conf具体的配置

/var/log/mongodb/*.log {
    daily
    size=1K
    rotate 180
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
        if [ -f /var/run/mongodb/mongod.pid ]; then
            kill -USR1 $(cat /var/run/mongodb/mongod.pid)
        fi
    endscript
}

/etc/anacrontab制定cron.daily等的执行时间

# cat /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

ls /etc/cron.daily/ 可以看到每日执行的任务

# cat /etc/cron.daily/logrotate
#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

我们用 cron.daily 工作来说明一下 /etc/anacrontab 的执行过程:

读取 /var/spool/anacron/cron.daily 文件中 anacron 上一次执行的时间。
和当前时间比较,如果两个时间的差值超过 1 天,就执行 cron.daily 工作。
只能在 03:00-22:00 执行这个工作。
执行工作时强制延迟时间为 5 分钟,再随机延迟 0~45 分钟。
使用 nice 命令指定默认优先级,使用 run-parts 脚本执行 /etc/cron.daily 目录中所有的可执行文件。

你可能感兴趣的:(logrotate)