在服务中调用 sd_notify(0, "WATCHDOG=1")

Calling a sd_notify(0, "WATCHDOG=1") in a service

我有一个系统服务。我想为此实施一个看门狗。 有点像,

[Unit]
Description=Watchdog example service

[Service]
Type=notify
Environment=NOTIFY_SOCKET=/run/%p.sock
ExecStartPre=-/usr/bin/docker kill %p
ExecStartPre=-/usr/bin/docker rm %p
ExecStart=/usr/libexec/sdnotify-proxy /run/%p.sock /usr/bin/docker run \
    --env=NOTIFY_SOCKET=/run/%p.sock \
    --name %p pranav93/test_watchdogged python hello.py
ExecStop=/usr/bin/docker stop %p

Restart=on-success
WatchdogSec=30s
RestartSec=30s


[Install]
WantedBy=multi-user.target

根据文档,我必须每隔指定的一半时间间隔调用 sd_notify("watchdog=1")(在本例中为 15s)。但我不知道如何在服务中调用该函数。将不胜感激。

sd_notify(0,"WATCHDOG=1") 是一个 API 用于通知 systemd 您的进程运行正常。

由于已使用 Type=notify,因此应在您未在服务中的应用程序中调用 sd_notify(0,"WATCHDOG=1"),并且必须定期调用(在 30 秒之前,因为您的服务中提到了 WatchdogSec=30s文件)以便 systemd 得到通知,

systemd 会将此视为失败的服务,因此 systemd 会终止您的服务并重新启动它。

示例服务文件

[Unit]
Description=Test watchdog Demo process
DefaultDependencies=false
Requires=basic.target

[Service]
Type=notify
WatchdogSec=10s
ExecStart=/usr/bin/TestWatchDogProcess
StartLimitInterval=5min
StartLimitBurst=5
StartLimitAction=reboot
Restart=always

testWatchDogProcess.c 的示例代码:

#include "systemd/sd-daemon.h"
#include <fcntl.h>
#include <time.h>

/* This should be sent once you are done with your initialization */
/* Until you call this systemd will keep your service as activating status */
/* Once you called, systemd will change the status of ur service to active */

sd_notify (0, "READY=1");

/* Way to get the WatchdogSec value from service file */
env = getenv("WATCHDOG_USEC");
if(env != NULL)
int interval = atoi(env)/(2*1000000);

/* Ping systsemd once you are done with Init */
sd_notify (0, "WATCHDOG=1");

/* Now go for periodic notification */
while(isRun == true)
{
    sleep(interval);
    /* Way to ping systemd */
    sd_notify (0, "WATCHDOG=1");
}

    return 0;

}

注意:根据您的 systemd 版本,请注意在编译期间包含正确的头文件和库。

我必须安装 systemd 库:

sudo apt-get install libsystemd-dev

并编译传递给链接器的程序:

gcc testWatchDogProcess.c -o testWatchDogProcess -lsystemd

我对@rameshrgtvl 代码进行了一些更改,使其直接 运行 而没有任何警告或错误。

#include <systemd/sd-daemon.h>
#include <fcntl.h>
#include <time.h>
/* This should be sent once you are done with your initialization */
/* Until you call this systemd will keep your service as activating status */
/* Once you called, systemd will change the status of ur service to active */

#define true  1

int main ()
{
        sd_notify (0, "READY=1");

        /* Way to get the WatchdogSec value from service file */
        char * env;
        int interval=0;
        int isRun = true;
        env = getenv("WATCHDOG_USEC");
        if (env)
        {
                interval = atoi(env)/(2*1000000);
        }
        /* Ping systsemd once you are done with Init */
        sd_notify (0, "WATCHDOG=1");

        /* Now go for periodic notification */
        while(isRun == true)
        {
                sleep(interval);
            /* Way to ping systemd */
                sd_notify (0, "WATCHDOG=1");
        }
        return 0;
}