码迷,mamicode.com
首页 > 其他好文 > 详细

nginx日志切割

时间:2017-07-13 18:59:38      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:www   crontab   路径   成功   erro   dai   cal   ota   作用   

作用:
如果日志文件比较大,MV是需要时间的,这段时间内的日志有可能丢失。logrotate可以在切割日志的同时,不丢失数据。

vi /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
daily
rotate 7
missingok
notifempty
dateext
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
endscript
}
脚本解释:
/usr/local/nginx/logs/*.log { nginx的日志路径
/usr/local/nginx/logs/www.qq.com.log error.log { 可以指定日志,用空格分开
daily 每天轮询
rotate 7 保留7天
missingok 如果日志丢失,不报错继续滚动下一个日志
notifempty 当日志为空时不进行滚动
dateext 在文件末尾添加当前日期
sharedscripts 运行postrotate脚本
postrotate 脚本内容,给NGINX发信号要求重新生成日志,如果nginx没开起来就不操作
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
endscript
}
测试,有生成就代表配置成功
/usr/sbin/logrotate -f /etc/logrotate.d/nginx
配置crontab
00 00 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx
也可以放入/etc/cron.daily/

一定要注意:1、在测试切割是否成功,一定要先备份一下!如果想保留全部日志,设置一个很大的值比如3650,如果你 注释rotate或者不加,那么不会切割成功。

nginx日志切割

标签:www   crontab   路径   成功   erro   dai   cal   ota   作用   

原文地址:http://www.cnblogs.com/lidong94/p/7161748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!