首页域名资讯 正文

logrotate轮询nginx日志

2024-11-27 2 0条评论

Linux日志文件如果不定期清理,会填满整个磁盘。这样会很危险,因此日志管理是系统管理员日常工作之一。我们可以使用”logrotate”来管理linux日志文件,它可以实现日志的自动滚动,日志归档等功能。下面以nginx日志文件来讲解下logrotate的用法。

在/etc/logrotate.d/目录下创建一个配置文件”nginx”,内容如下:

#vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
daily
rotate 5
missingok
dateext
compress
notifempty
sharedscripts
postrotate
    [ -e /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` ]
endscript
}

注释:

/usr/local/nginx/logs/*.log:需要轮询日志路径

daily:每天轮询

rotate 5:保留最多5次滚动的日志

missingok:如果日志丢失,不报错继续滚动下一个日志

dateext:使用日期作为命名格式

compress:通过gzip压缩转储以后的日志

notifempty:当日志为空时不进行滚动

/var/run/nginx.pid: nginx pid位置,请查看nginx.conf

postrotate/endscript:在截断转储以后需要执行的命令

立即截断可执行下面

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

注:

由于logratate已经加到cron.daily(/etc/cron.daily/logrotate),不再需要加到计划任务中

文章版权及转载声明

本文作者:亿网 网址:https://edns.com/ask/post/150133.html 发布于 2024-11-27
文章转载或复制请以超链接形式并注明出处。