Nginx reload log configuration

  Recently, I wrote a nginx log cutting script. After cutting, I found that the command to reload the log configuration file directly without restarting the service [  kill -USR1 $nginx.pid  ], but I don't know what the -USR1 parameter means. Finally, look for other people's experience on the Internet, not much to say, guest officer, you can look down.   

 

------------------------------- I am the dividing line --------------- ----------------

Before executing kill -USR1 $nginx.pid, even if the mv command has been executed on the file and the file name has been changed, nginx will still write log data to the newly named file "xxx.log_ 20130909" as usual. The reason is: in the Linux system, the kernel finds files based on file descriptors. 

1. Understanding of Linux file descriptors A 
file descriptor is an integer identifier named by the Linux kernel for each open file. 

The Linux kernel generates (or maintains) a "file descriptor table" for each process, and this file descriptor table records "files opened by this process (for identification)". 

In this environment, nginx is a running process. This process has already opened a log file, and the file is recorded in the file descriptor table. 

Even if the path of the log file is changed, it can still be found (located according to the file descriptor table).

 

2. Explanation of kill -USR1 $nginx.pid

In the linux system, linux communicates with the "running process" through signals. In the Linux system, there are also many predefined signals, such as SIGHUP. USR1 is a user-defined signal. It can be understood as: the process itself defines what to do when it receives this signal (that is, the process writer decides what to do when it receives this signal or does nothing, and it is completely left to the developer to decide). And in nginx, it wrote its own code to handle when I received the USR1 signal, let nginx reopen the log file. The specific principles are as follows: 

1. When the main process of nginx receives the USR1 signal, it will reopen the log file (named after the log name in the nginx configuration file, which is the value set by the access_log item in the configuration file. If the file does not exist, it will be automatically created. a new file xxx.log). 

2. Then change the owner of the log file to "worker process (worker process)", the purpose is to allow the worker process to have read and write permissions to the log file (master and worker usually run as different users, so the owner needs to be changed ). 

3. The nginx main process will close the log file with the same name (that is, the file renamed to xxx.log_ 20130909.log using the mv command just now), and notify the worker process to use the newly opened log file (the file xxx opened by the main process just now) .log). The specific implementation is more detailed. The main process sends the USR1 signal to the worker. After the worker receives this signal, it will reopen the log file (that is, the xxx.log agreed in the configuration file)

So -USR1 is a signal of Nginx, so what other signals are there and what do these signals do? Summarized as follows:

The main process can handle the following signals:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325131453&siteId=291194637