What's the easiest way to rotate nginx log files monthly?

夙愿已清 提交于 2021-02-08 09:20:07

问题


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.

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

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?


回答1:


I think the following crontab should work:

0   0   1   *   *   /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`

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 »).




回答2:


please check also this misc@ thread.

(also keep in mind the caveat documented in the FAQ about privseped apache and the need for a small time window upon the move.)



来源:https://stackoverflow.com/questions/15183321/whats-the-easiest-way-to-rotate-nginx-log-files-monthly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!