CentOs7的logrotate拆分nginx日志

  •   
  • 1756
  • Linux
  • 0
  • super_dodo
  • 2020/11/25

logrotate作为一个/etc/cron.daily/logrotate任务每日定时执行。
你往/etc/logrotate.d/下添加配置文件 logrotate执行时会自动根据配置文件进行日志归档。
比如apt-get安装的Nginx官方源的日志归档配置文件/etc/logrotate.d/nginx内容为:

/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] &amp;&amp; kill -USR1 <code>cat /var/run/nginx.pid</code>
endscript
}

配置说明:

daily: 日志文件每天进行滚动
missingok: 如果找不到这个log档案,就忽略过去
rotate: 保留最进52次滚动的日志
compress: 通过gzip压缩转储以后的日志
delaycompress: 和compress一起使用时,转储的日志文件到下一次转储时才压缩
notifempty: 如果是空文件的话,不转储
create mode owner group:转储文件,使用指定的文件模式创建新的日志文件
sharedscripts: 运行postrotate脚本(该脚本作用为让nginx重新生成日志文件)
postrotate/endscript: 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行