阻止服务启动

Prevent service from starting

想法是创建一个永远不会启动的 keep-down.service

[Unit]
Description=Keeps daemon down

Before=mydaemon.service
ConditionPathIsDirectory=/var/lib/dummy-never-existing-path

[Service]
ExecStart=/bin/true

[Install]
WantedBy=multi-user.target

所以 mydaemon.service 需要它。安装 keep-down.service 时,mydaemon.service 无法启动(理论上)。

动机:

问题是 mydaemon.service 无论如何都会启动。这是为什么?

您已指定 keep-down 服务必须在 之前启动 mydaemon.service:

Before=mydaemon.service

但是你实际上并没有配置两者之间的任何依赖关系,比如:

RequiredBy=mydaemon.service

详细信息在 systemd.unit 手册页中,例如关于 Before=After=:

Note that this setting is independent of and orthogonal to the requirement dependencies as configured by Requires=. It is a common pattern to include a unit name in both the After= and Requires= option, in which case the unit listed will be started before the unit that is configured with these options.

然而,这一切实在是大可不必。您可以使用 systemctl mask 命令完成您想要的操作:

   mask NAME...
       Mask one or more unit files, as specified on the command line. This
       will link these units to /dev/null, making it impossible to start
       them. This is a stronger version of disable, since it prohibits all
       kinds of activation of the unit, including enablement and manual
       activation. Use this option with care. This honors the --runtime
       option to only mask temporarily until the next reboot of the
       system. The --now option can be used to ensure that the units are
       also stopped.

所以:

# systemctl mask mydaemon