我有两个服务A.service和B.service.当A退出/被杀/被停止时,我希望B被停止.此外,我希望B重新启动时重新启动.我尝试了给出的答案
How to start and stop a systemd unit with another?并按如下方式设置我的文件: –
How to start and stop a systemd unit with another?并按如下方式设置我的文件: –
A.service
[Unit] Description=A Before=B.service Requires=B.service [Service] ExecStart=/usr/share/A ExecStopPost=/usr/evo/exit_handler.sh %p Restart=always StartLimitBurst=3 StartLimitInterval=300
B.service
[Unit] Description=B BindsTo=A.service [Service] Type=forking ExecStart=/usr/share/B start ExecStop=/usr/share/B stop StartLimitBurst=5 StartLimitInterval=10 Restart=always
当我杀死A或执行systemctl重启A时,我看到B重启了.但是当A退出状态为0时,我看不到B重新启动.我启用了systemd调试登录,我发现A的状态变为dead但不是自动重启
解决方法
你的B单位正在使用BindsTo =,它用于跟踪可能消失的单位的状态.
你需要的是PartOf =,而这个服务将精确地跟踪命名服务的状态.从documentation:
PartOf=
Configures dependencies similar toRequires=
,but limited to stopping and restarting of units. When systemd stops or restarts the units listed here,the action is propagated to this unit. Note that this is a one-way dependency — changes to this unit do not affect the listed units.
但我不认为这会解决你所有的问题.我怀疑你没有仔细考虑你的两个服务之间的实际依赖关系,并且需要做一些更多的思考(并且能够表达自己),然后才能让这些单元做他们需要做的事情.