logrotate配置maxsize不生效

环境:

  • CentOS 7, 64位系统
  • logrotate 3.8.6 - Copyright © 1995-2001 Red Hat, Inc.

问题描述

$ cat /etc/crontab
*/10 * * * * root /etc/cron.daily/logrotate

$ 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


$ cat /etc/logrotate.conf 
weekly
rotate 4
create
dateext
include /etc/logrotate.d

...

# system-specific logs may be also be configured here.

$ cat /etc/logrotate.d/xx
"/var/log/xx.log" {
  daily
  rotate 1
  notifempty
  maxsize 100M
  nocompress
  nomissingok
  copytruncate
  su root root
}

其中 /etc/logrotate.conf 采用默认配置,
自己的 /var/log/xx.log 配置daily, maxsize 100M, 这样会导致:
xx.log一天内最多只滚动一次(xx-yyyy-mm-dd.log),
一天内, 当已经有xx-yyyy-mm-dd.log时,
就算xx.log增长超过了maxsize设置的值, 也不会翻转.

感觉这就是logrotate的bug, 因为它的man手册这样描述maxsize配置项:

maxsize size
Log files are rotated when they grow bigger than size bytes even before the additionally specified time interval
(daily, weekly, monthly, or yearly). The related size option is similar except that it is mutually exclusive with
the time interval options, and it causes log files to be rotated without regard for the last rotation time. When
maxsize is used, both the size and timestamp of a log file are considered.

大概是说:
当日志文件增长到比maxsize大时就会翻转, 即使在指定的时间间隔之前(daily, weekly, monthly, or yearly).

后来找到的解决方法, 有两种:

  1. 使用dateformat -%Y%m%d-%s
  2. 删除全局(/etc/logrotate.conf)的dateext设置, 这样的话会生成xx.1.log这样的格式, 并且每超过maxsize就会滚动(删除原来的xx.1.log, 生成新的xx.1.log)

实际中使用的是第二种, 第一种试过也可以.

参考

祝福

马上就是2020年了, 祝大家新的一年心想事成, 身体健康.

发布了231 篇原创文章 · 获赞 77 · 访问量 52万+

猜你喜欢

转载自blog.csdn.net/butterfly5211314/article/details/103787387