当我们重新部署应用程序时,我们习惯于systemd restart
重新启动应用程序。看起来这两种方法systemd restart
都不systemd status
可靠。
- 有时
systemd restart
不重新启动应用程序并且不会抛出错误。PID 与以前相同,并且旧版本的应用程序仍在运行。restart
立即生效吗? - 有时确实
systemd restart
会终止旧进程并启动新进程,但systemd status
服务却显示已停止。我们看到该进程作为 systemd 的子进程运行。
以第二个问题为例:systemctl status
显示服务处于不活动状态(已死亡)但它肯定正在运行(新的 pid 25548 和 25551)!
$systemctl status gateway.service
gateway.service - The API Gateway - Nginx
Loaded: loaded (/etc/systemd/system/gateway.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2020-08-05 14:00:16 CEST; 1h 43min ago
Process: 25545 ExecStart=/usr/sbin/nginx -c /prg/nginx/conf/gateway.conf (code=exited, status=0/SUCCESS)
Process: 25542 ExecStartPre=/usr/sbin/nginx -t -c /prg/nginx/conf/gateway.conf (code=exited, status=0/SUCCESS)
Process: 25540 ExecStartPre=/usr/bin/rm -f /daten/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 25545 (code=exited, status=0/SUCCESS)
Tasks: 2
Memory: 130.0M
CGroup: /system.slice/gateway.service
├─25548 nginx: master process /usr/sbin/nginx -c /prg/nginx/conf/gateway.conf
└─25551 nginx: worker process
ps | grep nginx
显示 2 个 nginx 进程:
nginx 25548 root 5u IPv4 58124367 0t0 TCP *:8080 (LISTEN)
nginx 25551 nginx 5u IPv4 58124367 0t0 TCP *:8080 (LISTEN)
pstree
显示这两个进程是 systemd 的子进程:
systemd─┬...
├─nginx───nginx
systemd 配置
[Service]
Type=forking
PIDFile=/daten/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /daten/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /prg/nginx/conf/gateway.conf
ExecStart=/usr/sbin/nginx -c /prg/nginx/conf/gateway.conf
KillSignal=QUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true