What to do if the nginx.pid file is lost when restarting nginx

王林
Release: 2020-10-19 17:31:57
forward
5007 people have browsed it

What to do if the nginx.pid file is lost when restarting nginx

1. Stop operation of nginx

(Recommended tutorial: nginx tutorial)

The stop operation is through the nginx process This is achieved by sending a signal. The specific error message is as follows:

[root@yoodb.com ~]# /usr/local/nginx/sbin/nginx -s reload
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid” failed (2: No such file or directory)
Copy after login

Step 1:

Query the nginx main process number, the code is as follows:

ps -ef | grep nginx
Copy after login

In Find the master process in the process list, and its number is the main process number.

Step 2:

Send a signal to stop Nginx calmly:

kill -QUIT 主进程号
Copy after login

Stop Nginx quickly:

kill -TERM 主进程号
Copy after login

Force to stop Nginx, the code is as follows:

pkill -9 nginx
Copy after login

In addition, if the pid file storage path is configured in nginx.conf, the file will store the Nginx main process ID. If not specified, it will be placed in the nginx logs directory. With the pid file, there is no need to query the main process number of Nginx first, but directly send a signal to Nginx. The command code is as follows:

kill -信号类型 ‘/usr/nginx/logs/nginx.pid’
Copy after login

2. Smooth restart of nginx

If After changing the configuration, you can send a signal to Nginx and restart smoothly.

The smooth restart command code is as follows:

kill -HUP 住进称号或进程号文件路径
Copy after login

Or use

/usr/nginx/sbin/nginx -s reload
Copy after login

Note that after modifying the configuration file, it is best to check whether the modified configuration file is correct to avoid restarting Later, an error occurred in Nginx, which affected the stable operation of the server. The command code to determine whether the Nginx configuration is correct is as follows:

nginx -t -c /usr/nginx/conf/nginx.conf
Copy after login

or

/usr/nginx/sbin/nginx -t
Copy after login

3. Smooth upgrade of nginx

If the server is running Nginx needs to be carried out When upgrading, adding or deleting modules, we need to stop the server and make corresponding modifications, so that the server will stop serving for a period of time. Nginx can perform various upgrade actions without stopping the server without affecting the operation of the server.

Step 1:

If you upgrade the Nginx program, first replace the old program file with the new program. After compiling and installing, the new program will be compiled directly into the Nginx installation directory.

Step 2: Execute the command

kill -USR2 旧版程序的主进程号或进程文件名
Copy after login

At this time, the old Nginx main process will rename its process file to .oldbin, and then execute the new version of Nginx. The new and old Nginx run together to process requests.

At this time, you need to gradually stop the old version of Nginx. Enter the command:

kill -WINCH 旧版主进程号
Copy after login

Slowly the old working process will exit as the task is completed, and the new version of Nginx working process will gradually replace the old version. work process.

You can decide to use the new version or revert to the old version.

4. Start the new/old working process without overloading the configuration

kill -HUP 旧/新版主进程号
Copy after login

Close the old/new process calmly

kill -QUIT 旧/新主进程号
Copy after login

If an error is reported at this time, prompt If there are still processes that have not ended, use the following command to first close the old/new working process, and then close the main process number:

kill -TERM 旧/新工作进程号
Copy after login

Note that on the Alibaba Cloud server, process nginx -s stop and then start again. nginx -s reload always reports an error

nginx: [error] open() “/alidata/server/nginx/logs/nginx.pid” failed (2: No such file or directory)
Copy after login

Cause: the pid is lost after the nginx process is killed, and nginx -s reload cannot be started the next time. Reinstalling can solve the problem. You can also refer to the English explanation:

issued a nginx -s stop and after that I got this error when trying to reload it.
[error]: invalid PID number “” in “/var/run/nginx.pid”
That /var/run/nginx/pid file is empty atm.
What do I need to do to fix it?
nginx -s reload is only used to tell a running nginx process to reload its config. After a stop,
you don’t have a running nginx process to send a signal to.
Just run nginx (possibly with a -c /path/to/config/file)
Copy after login

How to use, the code is as follows:

nginx -c /path/to/config/file
Copy after login

The Linux system execution command is as follows:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
Copy after login

The above is the detailed content of What to do if the nginx.pid file is lost when restarting nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template