systemd sd_notify 服务通过 SIGHUP 重新加载

Fel*_*lix 5 c signals daemon systemd

我正在实现一个使用 sd_notify 的 systemd 服务。

sd_notify 文档中,规定sd_notify(0, "READY=1");一旦重新加载完成,此类服务必须发送。

这对于服务本身触发的重新加载是有意义的,但我想知道这如何影响用户触发的重新加载(systemctl reload),因为systemd.service 文档指出重新加载命令应该同步以等待重新加载完成。

/bin/kill -HUP $MAINPID我现在的问题是:我可以在通知服务上使用异步命令(如)并使用RELOADING=1READY=1来获得“阻塞”重新加载,即让 systemd 等待我的服务,或者在从 systemctl 重新加载的情况下 systemd 是否忽略这两个命令?

为了完整性:我的代码和我想要的:

void doReload() {
    sd_notify(0, "RELOADING=1");
    // operation that takes ca 5 seconds
    sd_notify(0, "READY=1");
}
Run Code Online (Sandbox Code Playgroud)

并且可以从内部代码调用此方法,也可以通过注册的信号处理程序调用该方法SIGHUP

systemd 单元的相关部分是:

[Service]
Type=notify
NotifyAccess=main
ExecStart=/usr/bin/testd
ExecReload=/bin/kill -HUP $MAINPID
Run Code Online (Sandbox Code Playgroud)

我想归档ExecReload同步完成的事情。

Max*_*uxa 1

Systemd 253 添加了以下服务类型notify-reload

\n
\n

notify-reload 的行为与notify 相同。然而,它以一种方式扩展了逻辑:当要求服务重新加载时,SIGHUP UNIX 进程信号被发送到服务的主进程。(可以通过 ReloadSignal= 调整要发送的信号,请参见下文。)启动重新加载过程时,服务预计会通过 sd_notify(3) 回复一条通知消息,其中包含“RELOADING=1”字段以及“ MONOTONIC_USEC=" 设置为\xce\xbcs 中的当前单调时间(即clock_gettime(2) 中的CLOCK_MONOTONIC),格式为十进制字符串。重新加载完成后,必须发送另一条通知消息,其中包含“READY=1”。使用此服务类型并实现此重新加载协议是提供 ExecReload= 命令来重新加载服务配置的有效替代方法。

\n
\n

单元文件示例:

\n
[Service]\nType=notify-reload\nNotifyAccess=main\nExecStart=/usr/bin/testd\n# In case you want to use a signal other than SIGHUP for reloading:\n#ReloadSignal=SIGUSR1\n
Run Code Online (Sandbox Code Playgroud)\n

示例(省略错误检查):

\n
[Service]\nType=notify-reload\nNotifyAccess=main\nExecStart=/usr/bin/testd\n# In case you want to use a signal other than SIGHUP for reloading:\n#ReloadSignal=SIGUSR1\n
Run Code Online (Sandbox Code Playgroud)\n