谁掳走了 nginx.pid 文件?

1、重载配置

  执行 nginx  -s   reload  命令,报错:找不到 nginx.pid 文件,无法打开。曾经屡试不爽的命令,此时,竟然失灵了? 刚开始,我一头雾水,有点丈二和尚摸不着头脑……

[root@127-0-0-1 nginx]# nginx -s reload
 nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory)
[root@127-0-0-1 nginx]

  问题一:为什么会报错?

  答案: reload命令需要通过nginx.pid获取进程号,会去找/var/run.nginx.pid文件 ,如果不存在,就报错了。

2、进程文件

   问题一:有关  pid(进程) 的配置在哪里?

[root@127-0-0-1 nginx]# vim nginx.conf

user  king;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

  答案:在 nginx.conf 文件中的 pid 参数。显然nginx.pid 文件,就应该存在 /var/run/ 目录下。

  问题二:nginx.pid 的生存周期?

  答案: 一般情况,重启nginx,就会在/var/run/下生成一个nginx.pid文件。关闭nginx 或 kill  nignx的主进程后,nginx.pid就会消失……

[root@127-0-0-1 run]# cd /var/run
[root@127-0-0-1 run]# find -name nginx.pid
[root@127-0-0-1 run]# 

 奇怪,还真没有 nginx.pid 文件。那么问题就来了,为什么nginx在启动的状态下,nginx.pid竟丢失了? 它去哪了? 还是谁掳走了它?

3、陷入深思

    nginx.pid 去哪了?怎么丢失的?

     1、误删  rm  -f  nginx.pid

     2、恶作剧    被哪个同事给耍了,还是城市套路深,我要回农村……

4、解决方案

   1)创建 nginx.pid 文件 —— 不是找不到文件么,我给你创建一个不得了嘛^_^

    nginx.pid存放的是nginx的master进程号。

   问题一: nginx 主进程号是多少?怎么查?

[root@127-0-0-1 run]# ps -ef|grep nginx
 king     11183 12666  0 14:45 ?        00:00:00 nginx: worker process
 king     11184 12666  0 14:45 ?        00:00:00 nginx: worker process
 king     11185 12666  0 14:45 ?        00:00:00 nginx: worker process
 king     11186 12666  0 14:45 ?        00:00:00 nginx: worker process
 root     11315 11042  0 15:43 pts/0    00:00:00 grep --color=auto nginx
 root     12666     1  0 Jun14 ?        00:00:00 nginx: master process nginx
[root@127-0-0-1 run]# 

 答案:nginx 主进程(master process)号:12666

[root@127-0-0-1 run]# touch nginx.pid
[root@127-0-0-1 run]# echo "12666" > nginx.pid
[root@127-0-0-1 run]# cat nginx.pid 
 12666
[root@127-0-0-1 run]#

 执行命令:nignx  -s  relaod 不报错了,问题解决了,喜悦心情油然而生……

[root@127-0-0-1 nginx]# nginx -s reload
[root@127-0-0-1 nginx]# 

 2)重启nginx —— 重启成功后,会生成一个nginx.pid文件。问题的克星——重启,是一把万能钥匙……

      步骤一:杀掉进程

           kill  -9  master 和 worker 进程号

      步骤二:重启nginx

          方法一:nginx

          方法二:nginx  -c  指定目录的配置文件    例:nginx  -c  /etc/nginx/nginx.conf

      步骤三:查看nginx.pid 文件

[root@127-0-0-1 nginx]# cd /var/run
[root@127-0-0-1 run]# find -name  nginx.pid
./nginx.pid
[root@127-0-0-1 run]# 

  步骤四:重载配置

          执行命令:nginx -s  reload ,不报错,问题解决了……

5、一顿瞎操作

  想法一:进程文件写入一个不存在的进程号,重载配置会怎么样?

[root@127-0-0-1 run]# echo "8888"> nginx.pid 
[root@127-0-0-1 run]# cat nginx.pid 
 8888
[root@127-0-0-1 run]# nginx -s reload
 nginx: [alert] kill(8888, 1) failed (3: No such process)
[root@127-0-0-1 run]#

 想法二:不杀进程,直接重启 nginx 会报错么?

[root@127-0-0-1 run]# nginx
 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 nginx: [emerg] still could not bind()
[root@127-0-0-1 run]# 

 报错:80端口,被占用。不是别人,是自己(nginx)……

 想法三:写错配置,会有什么影响?

[root@127-0-0-1 conf.d]# cat app.conf 
server {
  listen       80;  king   #wrong location 
  server_name  0.0.0.0:80;

   首先测试配置,然后重载

[root@127-0-0-1 conf.d]# nginx -t
 nginx: [emerg] unknown directive "king" in /etc/nginx/conf.d/HKIN-CRS.conf:3
 nginx: configuration file /etc/nginx/nginx.conf test failed
[root@127-0-0-1 conf.d]# nginx -s reload
 nginx: [emerg] unknown directive "king" in /etc/nginx/conf.d/HKIN-CRS.conf:3
[root@127-0-0-1 conf.d]# 

  

缘于生活,而归于工作。本人所书,而意于分享。 如有转载,请注明出处! --活出自己范儿
原文地址:https://www.cnblogs.com/Small-sunshine/p/11214305.html