用途
在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