【发布时间】:2018-05-30 00:10:44
【问题描述】:
我已经安装了 logrotate 3.8.6 的 CentOS 7.4。我在/etc/logrotate.d/ 下有一个自定义 logrotate 文件,用于轮换安装在同一台机器上的 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 cron 作业每天只调用一次,并且由于您已经有了
daily指令,maxsize应该没有任何效果。您希望大于 50M 的日志会立即轮换吗? -
我需要设置一个大小限制来强制轮换,因为当 catalina.out 日志达到 2GB 时,Tomcat 停止工作,这可能会在一天结束之前发生。从手册页中我读到:“即使在额外指定的时间间隔(每天、每周、每月或每年)之前,当日志文件变得大于 size 字节时,它们也会旋转。相关的大小选项是相似的,只是它与“ -
-
是的,但是 logrotate 仍然每天只调用一次。如果您想让
maxsize更频繁地加入,您必须将logrotate从/etc/cron.daily移动到/etc/cron.hourly。 -
但是我注意到到目前为止日志还没有达到50MB,并且已经过去了好几天没有任何轮换。
-
这很奇怪......他们不在
/opt/test/apache-tomcat-8.5.15-client/logs/backup?daily每天都应该把它们放在那里。