Nginx logs are separated by day

Source: Internet
Author: User

Preface
There are many ways to use kill-URS1 on the Internet to generate new log files for nginx, and many people complain that this method is correct. Here I want to tell you that this method is very correct. I have read the logrotates that comes with the system to achieve nginx log segmentation, view its bash source code, and found that it also sends the USR1 signal.
Here, I will explain the User-Defined signal USR1. After confirmation with Zhang Yang, a cool guy I especially admire, I found that most of the processes in ubuntu are responding to this signal by generating a log file again, and will not have any other impact on the current process.

Ideas
Use bash array and for to simplify log backup code
Use the suffix of the date function for log backup to achieve day-based splitting.
Use the USR1 signal to generate new logs for nginx

Bash script www.2cto.com
The log files to be backed up on my server are distributed in different directories, so my script has been properly optimized (you will understand the code carefully)
#! /Bin/bash-

#1. nginx log storage path
First_path = "/var/log/nginx/first"
Path_array = ($ first_path)

#2. log ID prefix Array
First_prefix_sign = "first"
Prefix_array = ($ first_prefix_sign)

#3. Log success error identification suffix Array
Bool_array = ("access" "error ")

#4. nginx log cutting backup suffix
Postfix = 'date-d' + 0 days '+ % Y % m % d' ". log"

#5. Back up the current log file and rename it to the date suffix
For path in $ {path_array [*]}
Do
For prefix in $ {prefix_array [*]}
Do
For bool in $ {bool_array [*]}
Do
File = $ path/$ prefix. $ bool. log
Backfile = $ path/$ prefix. $ bool. $ postfix
Echo $ file
Echo $ backfile
If [-e $ file]
Then
Mv $ file $ backfile
Fi
Done
Done
Done

#6. Search for the nginx process number to generate a new Log File
Nginx_pid = 'ps-aux | grep-E 'nginx: master process' | grep-v 'grep' | awk '{print $2 }''
# USR1: Reopen log files, refresh nginx log files
Kill-USR1 $ nginx_pid

Related Article

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.