Software engineering notes

Linux logrotate

(最後更新: 2016-04-27)

介紹

設定 log 定期壓縮及輩份,通常是 /var/log 下的 log,主要是控制 log 的大小不要越來越大

如何設定

有兩個地方可以設定

但不管寫在哪裡效果都是一樣的,因為 /etc/logrotate.d/ 下的檔案最後也是會被讀取到 /etc/logrotate.conf 裡執行

參數

語法參考

你也可以一次指定兩個 log 位置

/var/log/mysql.log /var/log/mysql/*log {

Apache /etc/logrotate.d/apache2 :

/var/log/apache/*.log {
        daily
        missingok
        rotate 30
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if /etc/init.d/apache2 status > /dev/null ; then \
                    /etc/init.d/apache2 reload > /dev/null; \
                fi;
        endscript
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi; \
        endscript
}

Nginx (預設) /etc/logrotate.d/nginx :

/var/log/nginx/*.log {
    weekly
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi \
    endscript
    postrotate
        invoke-rc.d nginx rotate >/dev/null 2>&1
    endscript
}

Rails production logrotate /etc/logrotate.d/rails :

/var/www/practice_rails/log/*.log {
  daily
  missingok
  rotate 7
  compress
  delaycompress
  notifempty
  copytruncate
}

何時生效

設定好之後,可以等明天或是執行強制 rotate

sudo logrotate -f /etc/logrotate.conf

如果設定的 logrotate 沒運作成功

使用 -v 啟動顯示模式,會顯示 logrotate 運作的過程

$ sudo logrotate -v /etc/logrotate.conf

reading config file /etc/logrotate.conf
including /etc/logrotate.d
reading config file apport
reading config file apt
reading config file aptitude
Ignoring test-program because of bad file mode.
reading config file dpkg
reading config file landscape-client

可以看到某一個被 Ignore 了, Ignoring test-program because of bad file mode.

這是因為 logrotate 要求你的 .conf 的 owner 是 root 且權限為 644, 如下:

-rw-r--r-- 1 root     ec2-user  146 Apr 17 09:35 test-program.conf

改完後再執行一次就可以正常運作了

手動觸發生效

command

sudo /etc/cron.daily/logrotate

讓 logrotate 執行更頻繁

預設執行 logrotate 的時間是每天, 所以即使 log 超過了設定的 size 也不會被 rotate, 要等到下一天執行時才會觸發

每小時執行: on Ubuntu, you can easily switch to hourly rotation by moving the script /etc/cron.daily/logrotate to /etc/cron.hourly/logrotate

每 5 mins 執行: add above to your /etc/crontab file. To run it every 5 minutes.

*/5 * * * * /etc/cron.daily/logrotate

reference