使用logrotate做nginx日志轮询

logrotate看名字就知道是专门做日志轮询的,只把任务配置放在/etc/logrotate.d/下,任务就会自动完成,而且无需安装,系统自带,比较推荐使用. 

Java代码 
  1. [root@srv logrotate.d]# vi /etc/logrotate.d/nginx  


配置如下

Java代码 
  1. /usr/local/nginx/logs/www.willko.cn.log /usr/local/nginx/logs/nginx_error.log {   
  2.         notifempty   
  3.         daily   
  4.         sharedscripts   
  5.         postrotate   
  6.                 /bin/kill -USR1 `/bin/cat /usr/local/nginx/nginx.pid`   
  7.         endscript   
  8.   
  9. }  
[java] view plain copy
 
  1. /usr/local/nginx/logs/www.willko.cn.log /usr/local/nginx/logs/nginx_error.log {  
  2.         notifempty  
  3.         daily  
  4.         sharedscripts  
  5.         postrotate  
  6.                 /bin/kill -USR1 `/bin/cat /usr/local/nginx/nginx.pid`  
  7.         endscript  
  8.   
  9. }  



多个日志以空格分开, 
notifempty 如果日志为空则不做轮询 
daily 每天执行一次 
postrotate 日志轮询后执行的脚本 

这样,每天都会自动轮询,生成nginx.log.1-n

posted @ 2016-09-11 15:07  王天泽博客  阅读(206)  评论(0编辑  收藏  举报