systemd如何向服务发送消息以引发sd_notify(3)响应?

问题描述 投票:0回答:1

我无法理解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个回答
1
投票

通常,systemd不会向您的守护程序发送消息。每次内部状态更改时,您的守护程序都应生成状态通知。否则,如果您的进程挂起(暂时或永久),则用户的状态请求将不会执行任何操作。请注意,还会显示守护程序的输出,因此可能没有必要始终调用sd_notify

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

© www.soinside.com 2019 - 2024. All rights reserved.