我有一个系统服务。我想为此实施看门狗。 它就像是,
[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
)。但我不知道如何在服务中调用该函数。帮助将受到高度赞赏。
答案 0 :(得分:9)
我必须安装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;
}
答案 1 :(得分:5)
sd_notify(0,"WATCHDOG=1")
是一个API,用于通知systemd您的流程运行正常。
当使用Type=notify
时,应在您的应用程序中调用sd_notify(0,"WATCHDOG=1")
,而不是在服务中这必须定期调用(30秒之前,因为您的服务文件中提到了WatchdogSec = 30s)以便systemd得到通知,
systemd会将此视为失败的服务,因此systemd将终止您的服务并重新启动它。
答案 2 :(得分:5)
[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
#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版本,请注意在编译期间包含正确的标题和库。