Home  >  Article  >  Operation and Maintenance  >  What to do if the nginx.pid file is lost when restarting nginx

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

王林
王林forward
2020-10-19 17:31:574919browse

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)

Step 1:

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

ps -ef | grep nginx

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 主进程号

Stop Nginx quickly:

kill -TERM 主进程号

Force to stop Nginx, the code is as follows:

pkill -9 nginx

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’

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 住进称号或进程号文件路径

Or use

/usr/nginx/sbin/nginx -s reload

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

or

/usr/nginx/sbin/nginx -t

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 旧版程序的主进程号或进程文件名

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 旧版主进程号

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 旧/新版主进程号

Close the old/new process calmly

kill -QUIT 旧/新主进程号

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 旧/新工作进程号

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)

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)

How to use, the code is as follows:

nginx -c /path/to/config/file

The Linux system execution command is as follows:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete