用途
在Centos
系統中,logrotate
可根據用戶配置的規則檢測和處理日誌文件,配合Cron
讓處理定時化。
組成
/usr/bin/logrotate
程序位置/etc/cron.daily/logrotate
定時任務執行腳本/etc/logrotate.conf
全局配置文件/etc/logrotate.d
應用配置文件存放目錄,覆蓋全局配置
配置說明
weekly
每週處理日誌rotate 4
保留4個日誌文件create
創建新的日之間dateext
默認未加時間戳compress
默認不壓縮
Nginx日誌管理
配置文件
/data/wwwlogs/*nginx.log {
daily
rotate 30
missingok
dateext
compress
notifempty
sharedscripts
postrotate
[ -e /var/run/nginx.pid ] && kill -USR1 cat /var/run/nginx.pid
endscript
}
定時執行
- 路徑
/etc/cron.daily/logrotate - shell腳本
#!/bin/sh
/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0