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
Theme Stack designed by Jimmy