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