我有一个在 RHEL 6.3 中运行的 Apache 服务器(2.2),具有以下 logrotate 配置:
# Rule for Apache logs
/var/opt/apache/logs/*log {
compress
missingok
notifempty
daily
rotate 15
create 0640 www_adm apache
olddir /var/opt/apache/logs/rotated
sharedscripts
postrotate
/bin/kill -HUP `cat /var/opt/apache/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
# Rule for Apache-Application logs
/var/opt/apache/logs/*/*log /var/opt/apache/logs/*/*/*log {
daily
copytruncate
compress
missingok
dateext
dateformat .%Y-%m-%d
rotate 90
create 0640 www_adm apache
sharedscripts
postrotate
/bin/kill -HUP `cat /var/opt/apache/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
cron 作业每天都在运行,没有出现问题,但文件轮换没有按预期工作。我收到的文件附加了多个日期,而不是每天只有一个:
mysite-ssl.2015-10-31.2015-11-02.2015-11-04.2015-11-06.2015-11-08.log
mysite-ssl.2015-10-31.2015-11-02.2015-11-04.2015-11-06.2015-11-08.2015-11-10.log
mysite-ssl.error.2015-10-21.2015-10-23.2015-10-25.2015-10-27.2015-10-29.2015-10-31.2015-11-02.2015-11-04.2015-11-06.2015-11-08.log
mysite-ssl.error.2015-10-21.2015-10-23.2015-10-25.2015-10-27.2015-10-29.2015-10-31.2015-11-02.2015-11-04.2015-11-06.2015-11-08.2015-11-10.log
mysite-ssl.2015-11-10.log.gz
mysite.2015-11-10.log.gz
mysite-ssl.2015-10-24.2015-10-26.2015-10-28.2015-10-30.2015-11-01.2015-11-03.2015-11-05.2015-11-07.2015-11-09.log
mysite-ssl.2015-10-24.2015-10-26.2015-10-28.2015-10-30.2015-11-01.2015-11-03.2015-11-05.2015-11-07.2015-11-09.2015-11-11.log
mysite-ssl.2015-11-11.log.gz
mysite.2015-11-11.log.gz
此外,在日志文件中,logrotate 似乎没有执行任何其他操作(参见 head/tail):
[user@host mysite]$ head mysite.log ; echo "----------------" ; tail mysite.log
145.245.114.238 - - [11/Nov/2015:03:25:30 +0100] "GET / HTTP/1.1" 301 306
145.245.114.238 - - [11/Nov/2015:03:28:07 +0100] "GET / HTTP/1.1" 301 306
145.245.114.238 - - [11/Nov/2015:03:30:03 +0100] "HEAD / HTTP/1.1" 301 -
145.245.114.237 - - [11/Nov/2015:03:31:15 +0100] "HEAD / HTTP/1.1" 301 -
145.245.114.237 - - [11/Nov/2015:03:31:15 +0100] "HEAD /mysite HTTP/1.1" 302 -
145.245.114.238 - - [11/Nov/2015:03:31:15 +0100] "HEAD /mysite/index.html HTTP/1.1" 200 -
145.245.114.238 - - [11/Nov/2015:03:31:36 +0100] "GET / HTTP/1.1" 301 306
145.245.114.237 - - [11/Nov/2015:03:31:48 +0100] "GET / HTTP/1.1" 301 306
145.245.114.238 - - [11/Nov/2015:03:35:28 +0100] "GET / HTTP/1.1" 301 306
145.245.114.237 - - [11/Nov/2015:03:38:07 +0100] "GET / HTTP/1.1" 301 306
----------------
145.245.114.238 - - [07/Jan/2016:15:41:36 +0100] "GET / HTTP/1.1" 301 306
145.245.114.237 - - [07/Jan/2016:15:44:12 +0100] "GET / HTTP/1.1" 301 306
145.245.114.238 - - [07/Jan/2016:15:47:41 +0100] "GET / HTTP/1.1" 301 306
145.245.114.238 - - [07/Jan/2016:15:47:57 +0100] "GET / HTTP/1.1" 301 306
145.245.114.237 - - [07/Jan/2016:15:51:36 +0100] "GET / HTTP/1.1" 301 306
145.245.114.238 - - [07/Jan/2016:15:54:12 +0100] "GET / HTTP/1.1" 301 306
145.245.114.238 - - [07/Jan/2016:15:55:04 +0100] "HEAD / HTTP/1.1" 301 -
145.245.114.237 - - [07/Jan/2016:15:57:41 +0100] "GET / HTTP/1.1" 301 306
145.245.114.237 - - [07/Jan/2016:15:57:57 +0100] "GET / HTTP/1.1" 301 306
145.245.114.237 - - [07/Jan/2016:16:01:36 +0100] "GET / HTTP/1.1" 301 306
知道是什么原因导致这种行为吗?我的配置缺少什么吗?
答案1
logrotate 配置中第二个块的第一行匹配以“log”结尾的文件。
/var/opt/apache/logs/*/*log /var/opt/apache/logs/*/*/*log {
通常,当你旋转文件并压缩它们时,扩展名会变成.gz
。这意味着上面的行将不再匹配。
但是,您正在更改以下行中的文件扩展名:
extension .log
这会导致您的文件再次轮换。您需要删除此行以防止扩展名更改,或者olddir
像在第一个配置块中所做的那样将轮换的文件移动到另一个文件夹。
答案2
您正在将旧日志文件旋转为具有日期的文件和 .log
附加到末尾。
因此,匹配要轮换的文件的模式也匹配旧的日志文件,这些文件会再次轮换(因此会添加新的日期,并再次.log
附加)。
您需要确保旧的已轮换日志文件不会再次轮换。您已经为轮换日志文件使用了单独的目录,但该目录与/var/opt/apache/logs/*/*log
第二节中的模式相匹配。
通常,轮换的日志文件的最后都会显示日期,以防止出现此类问题。