Featured image of post 解决 /run/nginx.pid 的 No such file or directory 问题

解决 /run/nginx.pid 的 No such file or directory 问题

解决 /run/nginx.pid 的 No such file or directory 问题

在启动 Nginx 时会出现找不到 nginx.pid 的讯息

$ nginx -s reload

[error] 13265#13265: open() "/run/nginx.pid" failed (2: No such file or directory)

找到 nginx.pid 设定

/etc/nginx/nginx.conf 的 nginx 设定档案可以看到 pid 的设定,可以确定 pid 档案的设定是放在哪边

可能是设定在 /run/nginx.pid/var/run/nginx.pid,要看看自己的设定 nginx.pid 设定在哪,我这边是设定在 /run/nginx.pid

# /etc/nginx/nginx.conf
pid /run/nginx.pid;

nginx.pid 生命週期

一般状况在重启 nginx 就会产生 nginx.pid 的档案,关闭或删除 nginx 程序时 nginx.pid 档案就会不见

$ cd /run
$ find -name nginx.pid

所以在 nginx 服务启动时理论上应该就会有 nginx.pid 的档案,但若这个档案不见的话可能会有几种原因

  1. 误删 rm -f nginx.pid
  2. 被同事恶搞???

解决找不到 nginx.pid 问题

1. 找到 nginx master 程序编号

使用 ps -ef | grep nginx 指令找出目前 nginx 执行中的程序有哪些

$ ps -ef | grep nginx
ps -ef | grep nginx
root     12964     1  0 03:27 ?        00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf
www-data 13614 12964  0 03:47 ?        00:00:00 nginx: worker process

上面可以找到 nginx: master 的主要程序编号是 12964

root     12964     1  0 03:27 ?        00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf

2. 建立 nginx.pid 档案

自己手动在 /run/nginx.pid 路径下建立 nginx.pid 档案,并将 nginx 主要程序编号写入到这个档案中

每个人自己主机的 nginx 程序编号不同,要自己查清楚

这裡将 nginx 主要程序编号写入 /run/nginx.pid 档案中

$ cd /run
$ touch nginx.pid
$ echo "12964" > nginx.pid
$ cat nginx.pid
12964

3. 重新启动 nginx

nginx.pid 建立完成后,重新载入 nginx,就不会看到 open() "/run/nginx.pid" failed (2: No such file or directory) 的讯息了

$ nginx -s reload

参考资料

comments powered by Disqus
All rights reserved,未經允許不得隨意轉載
Built with Hugo
主题 StackJimmy 设计