每月轮换 nginx 日志文件的最简单方法是什么? [英] What's the easiest way to rotate nginx log files monthly?

查看:78
本文介绍了每月轮换 nginx 日志文件的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 OpenBSD 中,ports 中没有 logrotatenewsyslog 就大量日志文件的每月轮换而言,似乎功能有限.

In OpenBSD, there's no logrotate in ports, and newsyslog seems to have limited features as far as monthly rotation of a huge number of log files is concerned.

我有很多域,大量的 nginx 日志文件名称,例如 /var/www/logs/*/*.{access,error}.log.

I have a lot of domains, a huge number of nginx log-files names like /var/www/logs/*/*.{access,error}.log.

我在想一个小的 shell 脚本和 cronjob.每月轮换它们并将前一个月附加到文件名的最简单方法是什么?

I'm thinking a small shell script and cronjob. What would be the easiest way to rotate them all monthly, and append the prior month to the filename?

推荐答案

我认为以下 crontab 应该可以工作:

I think the following crontab should work:

0   0   1   *   *   /etc/nginx/logrotate.monthly.sh

其中/etc/nginx/logrotate.monthly.sh 应该有以下内容:

Where /etc/nginx/logrotate.monthly.sh should have the following content:

find /var/www/logs/ -name "*log" -exec \
mv -i {} {}.`sh -c 'date -r $(expr $(date +%s) - 1209600) +%Y-%m'` \; ; \
kill -USR1 `cat /var/run/nginx.pid`

mv-i 选项对于确保文件不会被覆盖很重要.我们通过将今天的日期向后移动两周来获取文件名的日期(根据 « tcsh:在 shell 中打印两周前的日期 »).

The -i option to mv is important to ensure that files don't get overwritten. We get the date for the filename by moving today's date two weeks back (as per « tcsh: print date 2 weeks ago in shell »).

这篇关于每月轮换 nginx 日志文件的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆