利用logrotate切割nginx的access.log日志

时间:2023-03-09 17:11:41
利用logrotate切割nginx的access.log日志

一、新建一个nginx的logrotate配置文件


/var/log/nginx/access.log {
daily
rotate
compress
delaycompress
missingok
notifempty
create root root
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}

daily:日志文件将按田轮循。其它可用值为‘weekly’或者‘yearly’。

rotate 5: 一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除

compress: 在轮循任务完成后,已轮循的归档将使用gzip进行压缩

delaycompress:总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩 将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。

missingok: 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误

notifempty: 如果日志文件为空,轮循不会进行

create 644 root root: 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件

postrotate/endscript:在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd进程将立即再次读取其配置并继续运行

二、测试配置文件

三、添加crontab定时任务

  * * * run-parts /etc/cron/daily