Nginx重启报错: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)

       Today, because Alibaba Cloud's ECS cloud server memory is not enough, I directly upgraded the system configuration and restarted the system. Because the Nginx + Tomcat server configuration is installed in the system, I restarted Tomcat first, and then prepared to restart Nginx. Execute the command: /usr/local/nginx/sbin/nginx -s reload, the result failed to start, and an error was reported: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory). Could it be that the upgrade of the system has caused problems with the software that was originally running normally? I quickly checked the database and found that it was running normally. It seems that it should not be a big problem, so I slowed down to find the cause of the error.

       First of all, this error message is caused by the missing nginx.pid file. I searched for solutions on the Internet. One of the solutions is to create a new nginx.pid file; or adopt the second solution: the first step is to kill nginx; the second step It is to execute the startup command, and specify the path of the nginx configuration file through the parameter -c (after testing, it is also possible not to specify the configuration file here); the third step is to check whether the restart command reports an error.

[root@iZ2ze4ef9xk9hkxafq58d9Z ~]# pkill -9 nginx
[root@iZ2ze4ef9xk9hkxafq58d9Z ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@iZ2ze4ef9xk9hkxafq58d9Z ~]# /usr/local/nginx/sbin/nginx -s reload

       Although both solutions can solve the problem, some of them are superficial. Why are the previous good files missing?

       After testing by the author, execute the command on the console: '/usr/local/nginx/sbin/nginx -s stop' or '/usr/local/nginx/sbin/nginx -s quit' After successfully exiting nginx, '/usr/ local/nginx/logs/nginx.pid' file will be cleaned up automatically. Then continue to execute the restart command: /usr/local/nginx/sbin/nginx -s reload, it will report an error that the pid file cannot be found: [error] open() "/usr/local/nginx/logs/nginx.pid " failed (2: No such file or directory). Now continue to execute the normal startup command: /usr/local/nginx/sbin/nginx, and found that a new PID file has been created, so executing nginx -s reload again at this time will not prompt an error. According to the above test, it can be inferred that during the process of restarting the system, nginx exits and also clears the pid file, so after the system restarts, directly executing the nginx reload command will report an error because the pid file cannot be found.

       The reason behind the error was found. But what exactly does a PID file do? The main function of this file is to prevent users from starting multiple nginxes at the same time (the same is true for other PID files). How does nginx know that there is already running Nginx when it starts? Each Nginx is a process, and each process has a globally unique id number called pid. The process cannot detect other processes, so when nginx is started, it is not known whether there is currently nginx service. In order to be able to communicate, there must be something to transmit information. This thing is the pid file, which stores the id value of the process.

       When Nginx starts, it will first check whether there is an nginx.pid file, if not, create one, and write its own pid into it. If it is detected that there is already an nginx.pid file, but nginx detects that there is no process whose id is the same as the id value in the file, then the process pointed to by the pid does not exist, and nginx will start normally, and write the pid of this startup to On the contrary, if an nginx.pid file is detected and the process pointed to by the pid exists, it means that the nginx service already exists, and an error will be reported when starting the nginx service this time.

Finally, attach the nginx startup and exit commands:

//查看nginx是否已经启动
ps ajx|grep nginx
//启动
/usr/local/nginx/sbin/nginx
//重启
/usr/local/nginx/sbin/nginx -s reload
//退出
/usr/local/nginx/sbin/nginx -s quit
//退出
/usr/local/nginx/sbin/nginx -s stop

The difference between quit and stop two exit methods:

Quit is a graceful shutdown. Nginx finishes serving the open connections before shutdown

Quit is an elegant way to close. Nginx completes the accepted connection requests before exiting.

Stop is a quick shutdown where is terminates in between serving the connection

Stop is a fast shutdown, regardless of whether there are any requests in progress.

Reference article:

CommandLine | NGINX

What is a pid file?

Guess you like

Origin blog.csdn.net/crazestone0614/article/details/126632143