我的 rsyslog 和 imfile 出现问题,当我的一个日志文件被截断时,它会失败。
为了解决这个问题,我修改了 logrotate 脚本如下:
"/etc/logrotate.d/logrotate.elasticbeanstalk.tomcat7.conf":
mode: "000644"
owner: root
group: root
content: |
/var/log/tomcat7/* {
size 1M
rotate 5
missingok
compress
notifempty
copytruncate
dateext
dateformat %s
olddir /var/log/tomcat7/rotated
postrotate
(/sbin/service rsyslog stop && rm -f /var/lib/rsyslog/tomcat-catalina && /sbin/service rsyslog start 2>&1) || true
endscript
}
在 tomcat7 文件夹中有或多或少 15 个文件,除了 catalina.out 之外,它们都小于 1M,因此我希望 logrotate 在处理此文件时仅执行 postrotate 脚本,但它的作用如下:
[ec2-user@ixxxxxxx]$ sudo logrotate -v /etc/logrotate.d/logrotate.elasticbeanstalk.tomcat7.conf
reading config file /etc/logrotate.d/logrotate.elasticbeanstalk.tomcat7.conf
reading config info for /var/log/tomcat7/*
olddir is now /var/log/tomcat7/rotated
Handling 1 logs
rotating pattern: /var/log/tomcat7/* 1048576 bytes (5 rotations)
olddir is /var/log/tomcat7/rotated, empty log files are not rotated, old logs are removed
considering log /var/log/tomcat7/catalina.2015-03-11.log
log does not need rotating
considering log /var/log/tomcat7/catalina.2015-03-12.log
log does not need rotating
considering log /var/log/tomcat7/catalina.2015-03-13.log
log does not need rotating
considering log /var/log/tomcat7/catalina.2015-03-14.log
log does not need rotating
considering log /var/log/tomcat7/catalina.2015-03-16.log
log does not need rotating
considering log /var/log/tomcat7/catalina.2015-03-17.log
log does not need rotating
considering log /var/log/tomcat7/catalina.2015-03-18.log
log does not need rotating
considering log /var/log/tomcat7/catalina.out
log needs rotating
considering log /var/log/tomcat7/host-manager.2015-03-11.log
log does not need rotating
considering log /var/log/tomcat7/host-manager.2015-03-12.log
log does not need rotating
considering log /var/log/tomcat7/host-manager.2015-03-13.log
log does not need rotating
considering log /var/log/tomcat7/host-manager.2015-03-14.log
log does not need rotating
considering log /var/log/tomcat7/host-manager.2015-03-16.log
log does not need rotating
considering log /var/log/tomcat7/host-manager.2015-03-17.log
log does not need rotating
considering log /var/log/tomcat7/host-manager.2015-03-18.log
log does not need rotating
considering log /var/log/tomcat7/localhost.2015-03-11.log
log does not need rotating
considering log /var/log/tomcat7/localhost.2015-03-12.log
log does not need rotating
considering log /var/log/tomcat7/localhost.2015-03-13.log
log does not need rotating
considering log /var/log/tomcat7/localhost.2015-03-14.log
log does not need rotating
considering log /var/log/tomcat7/localhost.2015-03-16.log
log does not need rotating
considering log /var/log/tomcat7/localhost.2015-03-17.log
log does not need rotating
considering log /var/log/tomcat7/localhost.2015-03-18.log
log does not need rotating
considering log /var/log/tomcat7/localhost_access_log.txt
log does not need rotating
considering log /var/log/tomcat7/manager.2015-03-11.log
log does not need rotating
considering log /var/log/tomcat7/manager.2015-03-12.log
log does not need rotating
considering log /var/log/tomcat7/manager.2015-03-13.log
log does not need rotating
considering log /var/log/tomcat7/manager.2015-03-14.log
log does not need rotating
considering log /var/log/tomcat7/manager.2015-03-16.log
log does not need rotating
considering log /var/log/tomcat7/manager.2015-03-17.log
log does not need rotating
considering log /var/log/tomcat7/manager.2015-03-18.log
log does not need rotating
considering log /var/log/tomcat7/tomcat7-initd.log
log does not need rotating
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
rotating log /var/log/tomcat7/catalina.out, log->rotateCount is 5
Converted ' %s' -> '%s'
dateext suffix '1426682650'
glob pattern '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
copying /var/log/tomcat7/catalina.out to /var/log/tomcat7/rotated/catalina.out1426682650
truncating /var/log/tomcat7/catalina.out
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
compressing log with: /bin/gzip
removing old log /var/log/tomcat7/rotated/catalina.out1426648681.gz
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
running postrotate script
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
我该如何避免这种情况发生?我不是 Unix 方面的专家,但我猜像这样启动 100 次服务不太好?
答案1
要仅运行一次 postrotate 脚本(在多个文件上定义 logrotate 条目时),请添加sharedscripts
选项