Apache-2.2

Apache2 和 logrotate:需要延遲壓縮嗎?

  • February 8, 2016

我目前正在查看我的 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
}

如果您正在重新啟動 Apache(甚至是“優雅”),它將關閉打開的文件句柄並再次打開它們。您不應該需要延遲壓縮,因為該文件將作為 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)

重新啟動是個壞主意——如果配置文件意外更改並且不再有效怎麼辦?您的 apache 不會啟動備份。而是向父程序發送一個 HUP,告訴它關閉/重新打開文件句柄。

postrotate
 /bin/kill -HUP `cat /var/run/apache2.pid 2>/dev/null` 2>/dev/null || true
endscript

如果 PID 失去(或為空,或無效)導致 kill 也失敗,cat 將失敗,因此您不需要if..then它周圍的塊。

引用自:https://serverfault.com/questions/151014