pnv*_*pnv 5 watchdog notify systemd coreos
我有一个系统服务.我想为此实施看门狗.它是这样的,
[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
Run Code Online (Sandbox Code Playgroud)
根据文档,我要调用sd_notify("watchdog=1")指定间隔的每一半(在这种情况下是它15s).但我不知道如何在服务中调用该函数.帮助将受到高度赞赏.
我不得不安装systemd lib:
sudo apt-get install libsystemd-dev
Run Code Online (Sandbox Code Playgroud)
并编译将程序传递给链接器:
gcc testWatchDogProcess.c -o testWatchDogProcess -lsystemd
Run Code Online (Sandbox Code Playgroud)
我在@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;
}
Run Code Online (Sandbox Code Playgroud)
sd_notify(0,"WATCHDOG=1") 是用于通知systemd您的进程运行正常的API。
正如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
Run Code Online (Sandbox Code Playgroud)
#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;
}
Run Code Online (Sandbox Code Playgroud)
注意:根据您的systemd版本,请注意在编译期间包含正确的标题和库.
| 归档时间: |
|
| 查看次数: |
7369 次 |
| 最近记录: |