使用 Linux 系统自带的日志切割工具对 Nginx 进行日志切割
来源:靑龍一笑的博客 作者:靑龍一笑 发布时间:2015-11-14 12:52:01 点击量:761 评论:0
logrotate 是 linux 系统自带的日志文件管理工具,我们需要做的就是在 /etc/logrotate.d/ 目录下建立 Nginx 日志切割的配置文件。
首先,确认一下 Nginx 日志文件所在的路径:
[root@RicenOS logs]# pwd
/opt/nginx/logs
/opt/nginx/logs
接着,在 /etc/logrotate.d 目录下建立配置文件:
[root@RicenOS logs]# vi /etc/logrotate.d/nginx
内容如下:
/opt/nginx/logs/*.log {
daily
rotate 180
missingok
notifempty
dateext
compress
olddir /opt/nginx/logs/history
sharedscripts
postrotate
if [ -f /opt/nginx/logs/nginx.pid ];then
kill -USR1 `cat /opt/nginx/logs/nginx.pid`
fi
endscript
}
daily
rotate 180
missingok
notifempty
dateext
compress
olddir /opt/nginx/logs/history
sharedscripts
postrotate
if [ -f /opt/nginx/logs/nginx.pid ];then
kill -USR1 `cat /opt/nginx/logs/nginx.pid`
fi
endscript
}
测试:
[root@RicenOS logs]# logrotate -f /etc/logrotate.d/nginx
接下来要创建一个计划任务,让它每天凌晨 0 点执行。
创建一个 crontab:
[root@RicenOS logs]# crontab -e
内容如下:
0 0 * * * /sbin/logrotate -f /etc/logrotate.d/nginx
版权所有 © 2005-2023 靑龍一笑的博客 Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn All Rights Reserved.
Copyright © 2005-2023 by www.ricensoftwares.com.cn All Rights Reserved.