發(fā)現(xiàn)問(wèn)題
? ~ 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.
問(wèn)題原因
因?yàn)?nginx 啟動(dòng)需要時(shí)間比原,而 systemd 在 nginx 完成啟動(dòng)前就去讀取 pid file造成讀取 pid 失敗军拟。
解決方案
讓 systemd 在執(zhí)行 ExecStart 的指令后等待一點(diǎn)時(shí)間。
如果 nginx 啟動(dòng)需要時(shí)間更長(zhǎng)啊送,可以把 sleep 時(shí)間再改長(zhǎng)一點(diǎn)杀餐。
處理問(wèn)題
? ~ 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
處理結(jié)果
? ~ 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.
? ~