【问题标题】:Systemd target file starts eventhough the After= service is not started即使 After= 服务未启动,Systemd 目标文件也会启动
【发布时间】:2019-10-21 10:40:37
【问题描述】:

好的,首先,我想实现 systemd 服务不会被垃圾收集,如果它被停止并且默认情况下没有启用。 我需要这个是因为来自Prometheuscurrent problem with the node exporter 用于监控目的。

我确实已经完成了我的目标,但我不确定它为什么会起作用以及是否有可能的副作用。

例如,我在我的 Ubuntu 18.03 上安装了 ntp,禁用了该服务并停止了它。现在我想让我的节点导出器告诉我,ntp.service 是不活动的,但是 ntp get 的垃圾被 systemd 收集了。我读到,在这种情况下,可以通过在某处使用 ntp 作为依赖项来防止这种情况。使用Wants= 没有帮助,因为如果我的依赖服务重新启动,这将导致我的 ntp 重新启动。所以我做了一些实验,我创建了一个 test.target 文件,如下所示:

#/etc/systemd/system/test.target
[Unit]
Description=Testing purpose
After=ntp.service

[Install]
WantedBy=Multi-user.target

我启用了该服务并在禁用和停止 ntp 的情况下启动它。现在这是我的实际问题。即使 ntp.service 未运行,我的 test.target 文件确实已启用并正常启动。有趣的是,它也做了我想要的,ntp 不会被垃圾收集,即使它被禁用和停止。

所以我真的很想知道为什么会这样,为什么 After= 会被忽略?

【问题讨论】:

    标签: ubuntu prometheus systemd prometheus-node-exporter


    【解决方案1】:

    After= 未被忽略,它导致systemd 加载单元(如果尚未加载)并阻止垃圾收集。 After= 并不意味着它会启动ntp.service,因为你应该使用Requires=

    检查Unit Garbage CollectionRequiresAfter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 2021-01-08
      • 2015-07-15
      • 2015-10-03
      • 2020-05-11
      • 2015-11-17
      • 2015-06-13
      相关资源
      最近更新 更多