Nginx Log Cutting script

Source: Internet
Author: User

Nginx log file does not have the rotate function. If you do not process, log files will become larger and better, fortunately we can write an nginx log cutting script automatically cut log files.

The first step is to rename the log file, without worrying about renaming the log file and losing the log. When you do not reopen the original name of the log file, Nginx will still write to the file you renamed, Linux by the file descriptor rather than the file name location files.

The second step sends the USR1 signal to the Nginx main process.

After the Nginx main process receives the signal, it reads the log file name from the configuration file, re-opens the log file (named after the log name in the configuration file), and takes the user of the worker process as the owner of the log file.

After reopening the log file, the Nginx master process closes the log file with the same name and notifies the worker process to use the newly opened log file.

The worker process immediately opens a new log file and closes the log file with the name of the duplicate.

Then you can handle the old log files.

Nginx Log Automatic cutting script by date is as follows

#nginx日志切割脚本 #author:http://www.nginx.cn#!/bin/bash# Set the log file to store the directory Logs_path="/usr/local/nginx/logs/"#设置pid文件pid_path="/usr/local/nginx/nginx.pid"#重命名日志文件MV${logs_path}access.log ${logs_path}access_$ (Date-D"Yesterday"+"%y%m%d"). log# sends a signal to the Nginx main process to reopen the logKill-USR1 'Cat${pid_path} '

Save the above script nginx_log.sh

Crontab Setting up Jobs

0 0 * * * bash/usr/local/nginx/nginx_log.sh
In this way, the Nginx log is renamed to date format 0:0 every day and the new log file is rebuilt today.

Nginx Log Cutting script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.