nginx: [error] invalid PID number in nginx.pid 的解决办法

作者

nginx 修改配置文件后,重启的时候出现了,这个提示。 nginx: [error] invalid PID number "" in "/tmp/nginx.pid" 。 意思是说 不能在文件/tmp/nginx.pid中找到有效的 PID(进程ID) 。解决的办法有两种,一kill掉nginx 主进程,然后启动nginx 。 二: 把nginx的主进程的pid写入 nginx.pid文件,然后正常的重启。

nginx: [error] invalid PID number
nginx: [error] invalid PID number

一种方法杀掉nginx主进程,然后重启

# 重启前一定要先检查,配置文件是否正确,没有问题再重启
nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

# 关掉nginx 的所有进程
killall nginx

# 重新启动 nginx
nginx

把nginx的进程号重新写入 nginx.pid 中,然后正常重启

# 首先找到nginx进程 pid
ps aux | grep "nginx: master" | grep -v pts | awk '{print $2;}'

# 或者直接写入
#需要注意的是, nginx.pid 的文件的路径,不同的配置,可能路径是不同的。 可以从 nginx.conf 的主配置文件中获得。
echo `ps aux | grep "nginx: master" | grep -v pts | awk '{print $2;}'` > /tmp/nginx.pid

# 然后重启
nginx -s reload

找原因

nginx 重启的时候,需要知道,自身的PID是多少,而这个值是写在 nginx.pid 中,如果这个文件的值被修改或者删除,就会出现问题。这个值是可以被修改的,如果把里面的值修改成其他进程的PID,用root账号重启nginx的时候,拥有该PID的进程一样会被kill掉。

回复

您的电子邮箱地址不会被公开。