Apache2 和 logrotate:需要延迟压缩吗?

j0n*_*nes 8 compression logrotate apache-2.2

我目前正在查看 Apache 日志的文件大小,因为它们变得很大。在我的 logrotate 配置中,我已delaycompress启用。Apache 是否真的需要这个(因为 logrotate 文档说某些程序仍然在旧文件中写入)或者禁用它是否安全delaycompress

这是我的 logrotate 配置:

/var/log/apache2/*.log {
    weekly
    missingok
    rotate 26 
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
            if [ -f /var/run/apache2.pid ]; then
                    /etc/init.d/apache2 restart > /dev/null
            fi
    endscript
}
Run Code Online (Sandbox Code Playgroud)

小智 7

如果您正在执行 Apache 重新启动(或什至“优雅”),它将关闭打开的文件句柄并再次打开它们。您不应该需要 delaycompress,因为作为 postrotate 重新启动的一部分,该文件将已关闭并重新打开。

rotate access_log -> access_log.1 (rename action, no INODE change)
apache still writing to access_log.1 (same open FD on same INODE)
apache restart (close FD, release INODE writing)
apache writing to access_log (new FD to a new INODE)
Run Code Online (Sandbox Code Playgroud)

重新启动是一个坏主意 - 如果配置文件意外更改并且不再有效怎么办?您的 apache 不会启动备份。而是向父进程发送一个 HUP,它告诉它关闭/重新打开文件句柄。

postrotate
  /bin/kill -HUP `cat /var/run/apache2.pid 2>/dev/null` 2>/dev/null || true
endscript
Run Code Online (Sandbox Code Playgroud)

如果 PID 丢失(或为空,或无效)导致 kill 也失败,则 cat 将失败,因此您不需要if..then它周围的块。