logrotateによるログ分割およびスクロール処理

925 ワード

linux server上のサービスは一般的に長期にわたって実行され、サービスのログファイルは時間とともに大きくなり、ログがうまく処理されなければディスクがいっぱいになる可能性があります.幸いlogrotateというプログラムを見つけて処理しました.
インストール構成手順は次のとおりです.
zypper in -y logrotate

#  logrotate   cron    
cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

# logrotate.conf    /etc/logrotate.d  
cat /etc/logrotate.conf
...
include /etc/logrotate.d
...

# /etc/logrotate.d         nginx         
vim /etc/logrotate.d/nginx
/opt/nginx/logs/*.log {
    daily
    dateext
    compress
    rotate 3
    sharedscripts
    postrotate
        if [ -f /opt/nginx/logs/nginx.pid ]; then
           kill -USR1 `cat /opt/nginx/logs/nginx.pid`
        fi
    endscript
}

直接手動で実行することもできます
logrotate -d -f /etc/logrotate.d/nginx