首页  >  文章  >  后端开发  >  [nginx]每天自动切割日志脚本

[nginx]每天自动切割日志脚本

WBOY
WBOY原创
2016-08-08 09:21:461034浏览

nginx每天日志量比较大的时候,最好每天自动切割,存储,这样可以方面以后的查询和分析

<code><span>#!/bin/sh</span><span>###################</span><span>#filename: nginx_log_rotate.sh</span><span>#vsersion: 0.1v</span><span>#1 0 * * * /bin/sh /home/project/monitor/nginx_log_rotate.sh >/dev/null 2>&1</span><span>###################</span>logs_path=<span>"/usr/local/openresty/nginx/logs"</span>
old_logs_path=<span>${logs_path}</span>/old
nginx_pid=`cat /usr/local/openresty/nginx/logs/nginx.pid`

time_stamp=`date <span>-d</span><span>"yesterday"</span> +<span>"%Y-%m-%d"</span>`

mkdir -p <span>${old_logs_path}</span><span>#grep some mode file</span><span>for</span> file <span>in</span> `ls <span>$logs_path</span> | grep log$ | grep -v <span>'^20'</span>`
<span>do</span><span>if</span> [ ! <span>-f</span><span>${old_logs_path}</span>/<span>${time_stamp}</span>_<span>$file</span> ]
    <span>then</span>
        dst_file=<span>"<span>${old_logs_path}</span>/<span>${time_stamp}</span>_<span>$file</span>"</span><span>else</span>
        dst_file=<span>"<span>${old_logs_path}</span>/<span>${time_stamp}</span>_<span>$file</span>.$$"</span><span>fi</span>
    mv <span>$logs_path</span>/<span>$file</span><span>$dst_file</span><span>#gzip -f $dst_file  # do something with access.log.0</span><span>done</span>kill -USR1 <span>$nginx_pid</span></code>

版权声明:本文为orangleliu(http://blog.csdn.net/orangleliu/)原创文章,文章转载请声明。

以上就介绍了[nginx]每天自动切割日志脚本,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn