【发布时间】:2014-07-18 09:39:39
【问题描述】:
我想配置 logrotate 以将 /var/log/apache2/access.log 与写入 /var/log/apache2 的所有其他日志文件分开处理。如果不明确指定所有其他日志的名称,有没有办法让它工作?
我在网上看到 logrotate 使用 glob 对文件名进行模式匹配,但我似乎无法编写正确的模式。我尝试了很多模式,但它们都错误:
considering log /var/log/apache2/!(access.log)
log /var/log/apache2/!(access.log) does not exist -- skipping
上述模式在 bash 中确实有效,但仅适用于 shopt -s extglob。有没有办法使用 logrotate 来做到这一点,而不是让 apache 将 access.log 写入自己的目录,或者修改非访问日志文件的名称?
我还尝试通过使用 prerotate 脚本来使其工作,该脚本在尝试旋转 access.log 时失败,但 logrotate 抱怨我试图旋转 access.log 两次:
error: /etc/logrotate.d/apache2:16 duplicate log entry for /var/log/apache2/access.log
这是我得到的最接近的:
# rotate access.log every time logrotate is called
/var/log/apache2/access.log {
missingok
rotate 168
compress
create 640 root adm
# rotate the log every time we call logrotate (size 0)
size 0
dateext
dateformat .%Y%m%d.%s
copytruncate
}
# rotate everything daily EXCEPT access.log
/var/log/apache2/*.log {
daily
missingok
rotate 7
compress
create 640 root adm
copytruncate
# filter out access.log, which we want to rotate on its own schedule above
nosharedscripts
prerotate
bash -c "[[ ! '$1' =~ /access.log ]]"
endscript
}
【问题讨论】:
-
我有一个类似的问题,将“sudo /etc/cron.daily/logrotate”设置为 bash shell 并在该脚本中使用“shopt extglob”不起作用。 logrotate -d 报告
log /var/log/!(aaaa|bbb).log does not exist -- skipping