1. 程式人生 > >使用logrotate切割nginx日誌

使用logrotate切割nginx日誌

配置文件 local 切割

配置:
1、在/etc/logrotate.d目錄下創建一個nginx的配置文件"nginx"配置內容如下

#vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
daily
rotate 5
missingok
notifempty
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
endscript
}


保存退出。

2、執行logrotate

#/usr/sbin/logrotate -f /etc/logrotate.d/nginx


在/usr/local/nginx/logs目錄中會產生
error.log
error.log.1
說明logrotate配置成功。

3、讓logrotate每天進行一次滾動,在crontab中添加一行定時腳本。

#crontab -e
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx


每天23點59分進行日誌滾動

4、配置文件說明
daily:日誌文件每天進行滾動
rotate:保留最5次滾動的日誌
notifempty:日誌文件為空不進行滾動
sharedscripts:運行postrotate腳本
下面是一個腳本

postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
endscript


腳本讓nginx重新生成日誌文件。


參考博文:http://linux008.blog.51cto.com/2837805/555829/

使用logrotate切割nginx日誌