gpt4 book ai didi

linux - 阻止服务启动

转载 作者:太空宇宙 更新时间:2023-11-04 05:43:50 25 4
gpt4 key购买 nike

这个想法是创建一个永远不会启动的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 无法启动(理论上)。

动机:

  • 没有 systemctl 命令可以启动 mydaemon.service
  • 未修改 mydaemon.service 单元文件
  • 不要搞乱 mydaemon.service 的配置
  • 它是原子的:安装/删除 keep-down.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

关于linux - 阻止服务启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32740848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com