systemctl依赖失败,停止依赖服务

Systemctl dependency failure, stop dependent services

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

[Unit]
Description=My service

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

[Install]
WantedBy=multi-user.target,

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 显示状态失败)。现在有一个选项可以让 b.service 知道 a.service 失败了,它应该 stop/exit?

您想将 BindsTo=Requires= 添加到 [Unit] 部分,如 man systemd.unit:

中所述

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= 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.