创建脚本:
vim /etc/logrotate.d/nginx写入脚本内容:
/var/log/nginx/*.log{ daily rotate 30 copytruncate missingok compress dateext notifempty sharedscripts postrotate kill -USR1 `cat /var/run/nginx.pid` endscript #postrotate # sudo nginx -s reload>/dev/null 2>&1 #endscript }立刻执行脚本:
logrotate -vf /etc/logrotate.d/nginx添加计划任务:
crontab -e写入:
0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx上面是每天0点执行脚本
参数注释:
/var/log/nginx/*.log{ #日志存储位置 daily #按天切割 rotate 30 #保留30天的日志备份 copytruncate #这种方式操作的时候, 拷贝和清空之间有一个时间差,可能会丢失部分日志数据。 missingok #日志不存在分析,分析下一个 compress #转存之后压缩.tar.gz dateext #日志切割后,文件以当前日志为结尾,例如:access-logs-20181125 notifempty #空文件不转储 sharedscripts #整个日志组运行一次脚本 postrotate #这里到包裹的而是要执行的脚本命令 kill -USR1 `cat /var/run/nginx.pid` endscript #postrotate #从这里开始其实是不需要的只是另外一种实现让nginx重新创建日志文件的方式 # sudo nginx -s reload>/dev/null 2>&1 #endscript }这个方法能切割任意日志文件
除特别注明外,本站所有文章均为博文家原创,转载请注明出处来自https://www.32e.top/services/linux/article-170.html
暂无评论