【问题标题】:How does systemd send a message to a service to elicit a sd_notify(3) response?systemd 如何向服务发送消息以引发 sd_notify(3) 响应?
【发布时间】:2019-04-18 23:48:59
【问题描述】:

我无法理解 Systemd 的通知模型和几个工作流程。 Systemd 通知使用sd_notify (3) 接口发生。 sd_notify (3) 接口为守护进程提供了一种告诉 systemd 其状态的方法:

#include <systemd/sd-daemon.h>

int sd_notify(int unset_environment,
    const char *state);

int sd_notifyf(int unset_environment,
    const char *format,
    ...);

...

如果我正确解析了手册页,则守护程序应在启动并准备好处理数据后调用sd_notify(0, "READY=1\n");。当它停止时,它应该调用sd_notify(0, "STOPPING=1\n");

这是我遇到问题的工作流程之一。我看不到 Systemd 如何将消息传递给守护进程,说 “更新您的状态”,因此可以通过 systemctl status 向用户报告。

systemctl status mydaemon.service

我遇到问题的另一个工作流程是关机。我看不到 Systemd 如何将关闭消息传递给守护进程。

在这两种情况下,我都觉得我的可执行文件应该导出一个函数,而 Systemd 应该为查询和消息调用它。

Systemd 如何告诉守护进程报告其状态或关闭?

【问题讨论】:

    标签: c daemon systemd status shutdown


    【解决方案1】:

    一般来说,systemd 不会向您的守护进程发送消息。 每次内部状态发生变化时,您的守护程序都应该生成状态通知。否则,如果您的进程挂起(暂时或永久),用户的状态请求将不会做任何事情。请注意,还会显示守护程序的输出,因此可能不必总是调用sd_notify

    停止通常应由信号处理,或者可能使用ExecStop= 运行fooctl 进程,该进程通过套接字发送消息并等待回复。

    【讨论】:

    • 谢谢。 “每次内部状态更改时生成状态通知” 的问题在于,它只说明了过去某个时间的状态。它无法报告挂起的进程,这似乎是有人会systemctl status mydaemon.service 的原因。 (代码签名遇到同样的问题 - 证明仅适用于过去某个时间的某些代码。它没有说明当前正在执行的代码)。
    • 澄清一下,按照您描述的方式,您根本不会得到任何状态输出。通过它的实际工作方式,您只需获得最后一次更新,这对于回答“挂起时 在做什么 是什么”很有用。如果您想自己解决问题,请设置WatchdogSec=
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    相关资源
    最近更新 更多