使用 systemd 219 运行 RHEL7.X,我有一个名为“myService”的 systemd 服务,具有以下设置:
ExecStart=/bin/bash /asp_APPL/myInstance/script/myServiceSystemdCtrl.sh start
ExecStop=/bin/bash /asp_APPL/myInstance/script/myServiceSystemdCtrl.sh stop
KillMode=none
Restart=no
Type=notify
NotifyAccess=all
###Can't use ExitType until Systemd 250 release ( systemctl --version )
#ExitType=cgroup
当我调用 ExecStart 脚本时,它会运行许多不同的相互依赖的进程,并且我从启动脚本中向 systemd 发送一个带有 MAINPID= 的 NOTIFY 更新,并使用单个 PID 来监视我认为最重要的进程,systemctl 状态如下所示:
● [email protected] - "myService Instance : myInstance"
Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: disabled)
Active: active (running) since Thu 2022-06-16 15:19:40 BST; 23h ago
Process: 9700 ExecStop=/bin/bash /asp_APPL/myInstance/script/myServiceSystemdCtrl.sh stop (code=exited, status=0/SUCCESS)
Main PID: 6099 (myMainProcess)
有时这个 MAINPID 会失败并退出,但应用程序本身具有自我修复功能并且通常会恢复,这就是我们在 /etc/systemd/system/myService.service 定义文件中使用它的原因
KillMode=none
Restart=no
在这种“失败”状态场景中,我们得到以下 systemctl 状态,但这并不是主要后果,因为上面提到的自我修复,我们可以暂时接受这种不正确的状态
● [email protected] - "myService Instance : myInstance"
Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2022-06-17 14:58:44 BST; 1s ago
Process: 9700 ExecStop=/bin/bash /asp_APPL/myInstance/script/myServiceSystemdCtrl.sh stop (code=exited, status=0/SUCCESS)
Process: 6099 ExecStart=/bin/bash /asp_APPL/myInstance/script/myServiceSystemdCtrl.sh start (code=exited, status=1/FAILURE)
Main PID: 6099 (code=exited, status=1/FAILURE)
然而,有时应用程序无法恢复,我们希望通过必要的清理来“干净”地关闭所有服务,我们的 ExecStop 脚本做得很好。
然而,当我运行这个时,似乎没有任何东西运行,
systemctl stop myService@myInstance
但是,当我运行它时,我可以立即看到来自 ExecStart 的启动消息。
systemctl restart myService@myInstance
所以对我来说,当你处于“失败状态”时,ExecStop 脚本永远不会运行。
我什至尝试运行以下命令,但它对 ExecStop 也不起作用:
systemctl reset-failed myService@myInstance
● [email protected] - "myService Instance : myInstance"
Loaded: loaded (/etc/systemd/system/[email protected]; enabled; vendor preset: disabled)
Active: inactive (dead) since Fri 2022-06-17 15:30:16 BST; 8min ago
Process: 9700 ExecStop=/bin/bash /asp_APPL/myInstance/script/myServiceSystemdCtrl.sh stop (code=exited, status=0/SUCCESS)
Process: 27952 ExecStart=/bin/bash /asp_APPL/myInstance/script/myServiceSystemdCtrl.sh start (code=killed, signal=KILL)
Main PID: 27952 (code=killed, signal=KILL)
无论如何,我可以强制 ExecStop 脚本仅从 systemctl 命令/上下文运行。据了解,我可以手动运行 ExecStop 脚本的各个方面来进行清理,尽管考虑到脚本中的所有内部 systemd 语义,但很难解耦。