现象:
nginx restart 超时
nginx stop之后,80端口仍然被占用
在 kill调进程之后再启动
查看 systemctl status nginx
systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
百度结果:
因为 nginx 启动需要一点点时间,而 systemd 在 nginx 完成启动前就去读取 pid file
造成读取 pid 失败
解决方法很简单,让 systemd 在执行 ExecStart 的指令后等待一点点时间即可
如果你的 nginx 启动需要时间更长,可以把 sleep 时间改长一点
mkdir -p /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
systemctl restart nginx.service
结果
[root@yumsources yum]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/nginx.service.d
└─override.conf
Active: active (running) since Sat 2020-06-27 16:50:21 CST; 50s ago
Process: 24397 ExecStartPost=/bin/sleep 0.1 (code=exited, status=0/SUCCESS)
Process: 24393 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 24390 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 24389 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 24396 (nginx)
CGroup: /system.slice/nginx.service
├─24396 nginx: master process /usr/sbin/nginx
└─24398 nginx: worker process
Jun 27 16:50:21 yumsources systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jun 27 16:50:21 yumsources nginx[24390]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jun 27 16:50:21 yumsources nginx[24390]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jun 27 16:50:21 yumsources systemd[1]: Started The nginx HTTP and reverse proxy server.