在一个服务中调用sd_notify(0, "WATCHDOG=1")是什么意思?

8

我有一个系统d服务,我想为它实现一个看门狗。 类似于这样:

[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

根据文档,我需要每隔指定的时间间隔(在这种情况下为15秒)调用sd_notify("watchdog=1")。但是我不知道如何在服务中调用该函数。如果能提供帮助将不胜感激。

3个回答

13

我必须安装systemd lib:

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;
}

现在我看到那是你分享的代码,@rameshrgtvl。谢谢! - fabatera

8
sd_notify(0,"WATCHDOG=1")是一个API,用于通知systemd您的进程正在正常工作。
由于使用了Type = notify,因此应在应用程序中调用sd_notify(0,“WATCHDOG = 1”)而不是在服务中调用,并且必须定期调用此函数(在30秒之前调用,因为WatchdogSec = 30s在您的服务文件中已经被提到),以便systemd得到通知。否则,systemd将认为这是一个失败的服务,因此将终止并重新启动该服务。

8

示例服务文件

[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版本,请在编译时确保包含适当的头文件和库。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接