Centos

Logrotate daily+maxsize 不旋轉

  • April 14, 2022

我已經安裝了 logrotate 3.8.6 的 CentOS 7.4。我有一個自定義 logrotate 文件,/etc/logrotate.d/用於輪換安裝在同一台機器上的 Tomcat(例如 catalina.out)上的一些日誌。

/opt/test/apache-tomcat-8.5.15-client/logs/catalina.out {
copytruncate
daily
rotate 30 
olddir /opt/test/apache-tomcat-8.5.15-client/logs/backup
compress
missingok
maxsize 50M
dateext
dateformat .%Y-%m-%d
}

我希望日誌每天輪換,或者如果大小達到 50MB。發生這種情況時,日誌文件會被壓縮並複製到備份文件夾中,並在被刪除之前保留 30 天。

我已經使用以下命令在調試模式下手動執行 logrotate 並且沒有顯示錯誤(並且創建了預期的壓縮日誌文件):

/usr/sbin/logrotate -d /etc/logrotate.d/openncp-tomcat-backoffice 2> /tmp/logrotate.debug

/var/lib/logrotate/logrotate.status沒有問題的情況下,文件顯示為已旋轉,但實際上並非如此:

"/var/log/yum.log" 2017-11-27-19:0:0
"/opt/test/apache-tomcat-8.5.15-server/logs/catalina.out" 2017-12-15-3:41:1
"/var/log/boot.log" 2017-12-15-3:41:1
"/var/log/up2date" 2017-11-27-19:0:0

我有預設值/etc/logrotate.conf

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
   monthly
   create 0664 root utmp
       minsize 1M
   rotate 1
}

/var/log/btmp {
   missingok
   monthly
   create 0600 root utmp
   rotate 1
}

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

我也有預設值/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

根據logrotate 聯機幫助頁

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. 

我注意到,到目前為止,日誌還沒有達到 50MB,而且幾天過去了,沒有任何輪換。

我要求您指導如何適當地配置它。

該問題與日誌文件的 SELinux 文件類型有關,這些文件位於與 /var/log 不同的目錄中,這意味著 logrotate 程序無權執行其任務。我發現this other SO thread以及this Redhat page幫助解決了這個問題。我發現 Redhat 文件很有幫助,所以我在這裡提供了 2 個連結:

引用自:https://unix.stackexchange.com/questions/411444