Home  >  Article  >  Backend Development  >  [nginx] Automatically cut log script every day

[nginx] Automatically cut log script every day

WBOY
WBOYOriginal
2016-08-08 09:21:46994browse

nginx When the daily log volume is relatively large, it is best to automatically cut and store it every day, so as to facilitate future query and analysis

<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>

Copyright statement: This article is orangleliu (http://blog.csdn.net /orangleliu/) original article, please declare if the article is reprinted.

The above introduces the [nginx] daily automatic log cutting script, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn