加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 用另一个启动和停止一个systemd单元

发布时间:2020-12-13 16:46:28 所属栏目:Linux 来源:网络整理
导读:我有两个服务A.service和B.service.当A退出/被杀/被停止时,我希望B被停止.此外,我希望B重新启动时重新启动.我尝试了给出的答案 How to start and stop a systemd unit with another?并按如下方式设置我的文件: – A.service [Unit]Description=ABefore=B.se
我有两个服务A.service和B.service.当A退出/被杀/被停止时,我希望B被停止.此外,我希望B重新启动时重新启动.我尝试了给出的答案
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 to Requires=,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.

但我不认为这会解决你所有的问题.我怀疑你没有仔细考虑你的两个服务之间的实际依赖关系,并且需要做一些更多的思考(并且能够表达自己),然后才能让这些单元做他们需要做的事情.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读