[Solved] Nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument

First attach the error message:

 1 (myblog) root@Dapeng:/home/uwsgi# service nginx status 
 2 ● nginx.service - A high performance web server and a reverse proxy server
 3    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
 4    Active: active (running) since Thu 2019-05-02 13:49:36 CST; 29s ago
 5   Process: 17090 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
 6   Process: 17101 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 7   Process: 17094 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 8  Main PID: 17103 (nginx)
 9     Tasks: 2
10    Memory: 1.8M
11       CPU: 35ms
12    CGroup: /system.slice/nginx.service
13            ├─17103 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
14            └─17104 nginx: worker process                           
15 
16 May 02 13:49:36 Dapeng systemd[1]: Stopped A high performance web server and a reverse proxy server.
17 May 02 13:49:36 Dapeng systemd[1]: Starting A high performance web server and a reverse proxy server...
18 May 02 13:49:36 Dapeng systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
19 May 02 13:49:36 Dapeng systemd[1]: Started A high performance web server and a reverse proxy server.

My operating system is Ubuntu 16.04. After suddenly restarting nginx today, I found a 502 error reported to me. After looking at the status of nginx, there was an error prompt, just like the title. So Google found a link in a crooked nut’s personal blog, which solved my problem. I only work as a porter here, Small partners with problems can also refer to. Attached is the Ubuntu community reference link: bug for Ubuntu

This problem has been solved as a bug, but fortunately, there is a big solution

1 It seems to be a race between systemd and nginx. As if systemd was expecting the PID file to be populated before nginx had the time to create it.
2 
3 Workaround:
4 
5  mkdir /etc/systemd/system/nginx.service.d
6  printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf
7  systemctl daemon-reload

And then we can restart it

Similar Posts: