nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument

发现问题

➜  ~ service nginx start
➜  ~ service nginx status
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-03-11 16:28:42 CST; 4s ago
     Docs: man:nginx(8)
  Process: 22815 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 22829 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 22818 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 22833 (nginx)
    Tasks: 2 (limit: 2123)
   CGroup: /system.slice/nginx.service
           ├─22833 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─22836 nginx: worker process

Mar 11 16:28:42 VM-0-5-ubuntu systemd[1]: Stopped A high performance web server and a reverse proxy server.
Mar 11 16:28:42 VM-0-5-ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 11 16:28:42 VM-0-5-ubuntu systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Mar 11 16:28:42 VM-0-5-ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.

问题原因

因为 nginx 启动需要时间,而 systemd 在 nginx 完成启动前就去读取 pid file造成读取 pid 失败。

解决方案

让 systemd 在执行 ExecStart 的指令后等待一点时间。
如果 nginx 启动需要时间更长,可以把 sleep 时间再改长一点。

处理问题

➜  ~ mkdir /etc/systemd/system/nginx.service.d
➜  ~ printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf
➜  ~ systemctl daemon-reload
➜  ~ service nginx restart

处理结果

➜  ~ service nginx status
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/nginx.service.d
           └─override.conf
   Active: active (running) since Wed 2020-03-11 16:31:06 CST; 6s ago
     Docs: man:nginx(8)
  Process: 23374 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 23385 ExecStartPost=/bin/sleep 0.1 (code=exited, status=0/SUCCESS)
  Process: 23376 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 23375 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 23384 (nginx)
    Tasks: 2 (limit: 2123)
   CGroup: /system.slice/nginx.service
           ├─23384 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─23386 nginx: worker process

Mar 11 16:31:06 VM-0-5-ubuntu systemd[1]: Stopped A high performance web server and a reverse proxy server.
Mar 11 16:31:06 VM-0-5-ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 11 16:31:06 VM-0-5-ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
➜  ~

你可能感兴趣的:(nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument)