【问题标题】:Calling a sd_notify(0, "WATCHDOG=1") in a service在服务中调用 sd_notify(0, "WATCHDOG=1")
【发布时间】:2015-10-05 11:33:51
【问题描述】:

我有一个 sys 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

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

【问题讨论】:

    标签: notify coreos watchdog systemd


    【解决方案1】:

    我必须安装 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;
    }
    

    【讨论】:

    • 现在我知道是你分享代码了,@rameshrgtvl。谢谢!
    【解决方案2】:

    示例服务文件

    [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 版本,请注意在编译期间包含正确的头文件和库。

    【讨论】:

      【解决方案3】:

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

      由于Type=notify 已被使用,sd_notify(0,"WATCHDOG=1") 应该在您的应用程序中调用,并且必须定期调用(在您的服务文件中提到 WatchdogSec=30s 之前 30 秒)以便 systemd 获取否则通知,

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-26
        • 2011-06-20
        • 2017-11-28
        • 1970-01-01
        • 2015-01-26
        • 2014-07-28
        相关资源
        最近更新 更多