Systemctl依赖项失败,停止依赖项服务 [英] Systemctl dependency failure, stop dependent services

查看:864
本文介绍了Systemctl依赖项失败,停止依赖项服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个服务a.service和b.service. 显示a.service

I have 2 services a.service and b.service. a.service is shown

[Unit]
Description=My service

[Service]
Type=forking
ExecStart=/bin/sh /home/admin/run.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target,

b.service

b.service

[Unit]
Description=My service

[Service]
Type=forking
ExecStart=/bin/sh $HOME/theFolder/run.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

现在,当我启动b.service时,我确定a.service将被启动. 在运行时,突然有人弄乱了/home/admin/run.sh,而systemd无法启动a.service(而且systemctl status a.service显示为status失败).现在有一个选项,以便b.service可以知道a.service失败并且应该停止/退出吗?

Now, when i start b.service, i'm sure a.service will be started. During runtime, suddenly someone messes with /home/admin/run.sh and systemd is unable to start a.service (also systemctl status a.service shows failed as status). Now is there a option so that b.service can know that a.service is failed and it should stop/exit?

推荐答案

您要在[Unit]部分中添加BindsTo=Requires=,如man systemd.unit中所述:

You want to add either BindsTo= or Requires= to your [Unit] section, as documented in man systemd.unit:

要求= 配置对其他单元的需求依赖性.如果激活了此设备,则此处列出的设备 也将被激活.如果其他单元之一被停用或激活失败,则此单元 将被停用.可以多次指定此选项,或者可以使用多个以空格分隔的单位 在一个选项中指定,在这种情况下,将创建所有列出名称的需求依存关系. 请注意,需求依赖关系不会影响服务启动或停止的顺序. 必须使用After =或Before =选项独立配置.如果一个单元为foo.service 需要使用Requires =配置的bar.service单位,而After =或After没有配置任何订购 在==之前,则两个单元将同时启动,并且如果 foo.service已激活.通常,使用Wants =而不是Requires =是更好的选择,以便 实现一个在处理故障服务时更强大的系统.

Requires= Configures requirement dependencies on other units. If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or its activation fails, this unit will be deactivated. This option may be specified more than once or multiple space-separated units may be specified in one option in which case requirement dependencies for all listed names will be created. Note that requirement dependencies do not influence the order in which services are started or stopped. This has to be configured independently with the After= or Before= options. If a unit foo.service requires a unit bar.service as configured with Requires= and no ordering is configured with After= or Before=, then both units will be started simultaneously and without any delay between them if foo.service is activated. Often, it is a better choice to use Wants= instead of Requires= in order to achieve a system that is more robust when dealing with failing services.

BindsTo = 配置需求依赖关系,其样式与Requires =非常相似,但是除此之外 行为,它还声明当列出的任何单元突然消失时,该单元已停止. 如果服务自行终止,则设备可能突然,意外地消失. 不用systemd即可拔出插头或卸下挂载点.

BindsTo= Configures requirement dependencies, very similar in style to Requires=, however in addition to this behavior, it also declares that this unit is stopped when any of the units listed suddenly disappears. Units can suddenly, unexpectedly disappear if a service terminates on its own choice, a device is unplugged or a mount point unmounted without involvement of systemd.

这篇关于Systemctl依赖项失败,停止依赖项服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆