如何使用linux软件看门狗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9072879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:17:38  来源:igfitidea点击:

how to use linux software watchdog

linuxlinux-kernelbusyboxwatchdog

提问by Verve Innovation

Hi can anybody tell me how to handle the software watchdog in linux .I have a program "SampleApplication" which runs continuously and I need to restart it if its hangs or closes unexpectedly.

嗨,谁能告诉我如何在 linux 中处理软件看门狗。我有一个连续运行的程序“SampleApplication”,如果它意外挂起或关闭,我需要重新启动它。

I was googling about this and found linux has watchdog at /dev/watchdog but dont know how to use it.Could someone help me with example.

我在谷歌上搜索这个,发现 linux 在 /dev/watchdog 有看门狗,但不知道如何使用它。有人可以帮我举个例子。

My question is where to I specify my application name and delay interval to restart . As I am new to linux please brief me with sample if possible. Thanks

我的问题是在哪里指定我的应用程序名称和延迟间隔重新启动。由于我是 linux 新手,如果可能,请向我简要介绍示例。谢谢

采纳答案by Zan Lynx

Most of the Unix/Linux initprograms will manage daemons for you and restart them. Look into placing your service in /etc/inittab. Or you might be using Upstartor systemd.

大多数 Unix/Linuxinit程序将为您管理守护进程并重新启动它们。考虑将您的服务放在/etc/inittab. 或者您可能正在使用Upstartsystemd

All of these programs run as PID 1and it is their job to monitor and restart system processes.

所有这些程序都以这种方式运行,PID 1它们的工作是监视和重新启动系统进程。

From your Busybox tag I would assume you are running an embedded system. On those, the System V style init scripts with all of their shell scripts are really overkill. You should probably rip all that out and replace it with entries in /etc/inittabor upstart or systemd jobs.

根据您的 Busybox 标签,我假设您正在运行嵌入式系统。在这些方面,带有所有 shell 脚本的 System V 风格的 init 脚本真的有点矫枉过正。您可能应该删除所有这些内容,并将其替换为进入/etc/inittab或 upstart 或 systemd 作业的条目。

回答by Janne

How about using cron? Set up a small cron job that runs every minute. Check if your application is up (using ps) and if not, restart it.

使用cron怎么样?设置一个每分钟运行一次的小型 cron 作业。检查您的应用程序是否已启动(使用 ps),如果没有,请重新启动它。

Make a tiny script like this:

制作一个像这样的小脚本:

#!/bin/bash
if [ ! "$(pidof myapp)" ] 
then
  /path/to/myapp &
fi

You test if "myapp" is in the process list. "!" reverses the test. If it's not there, it runs "myapp". "&" is just so it starts in the background.

您测试“myapp”是否在进程列表中。“!” 反转测试。如果它不存在,它将运行“myapp”。“&”就是这样它在后台开始。

Add this to cron. Depending on your system and preferences there's several ways to do it. The classical one is to use crontab. There's lots of documentation on how to specify your crontab line, but you probably want something like this:

将此添加到 cron 中。根据您的系统和偏好,有几种方法可以做到。经典的一种是使用 crontab。有很多关于如何指定 crontab 行的文档,但您可能想要这样的东西:

* * * * * /path/to/the/script.sh > /dev/null

This will run your test every minute of every hour of every… You get the idea.

这将每分钟、每时每刻运行您的测试……您明白了。

回答by TJD

Documentation for the watchdog is here: http://linux.die.net/man/8/watchdog

看门狗的文档在这里:http: //linux.die.net/man/8/watchdog

But it sounds like this is not what you want. The linux software watchdog will reboot the machine, not just restart your process.

但听起来这不是你想要的。linux 软件看门狗将重新启动机器,而不仅仅是重新启动您的进程。

You can easily make your own watchdog. For example, you could have your program periodically write some temp file, and launch a script that checks the file once in a while and restarts your process if it hasn't updated for some time.

您可以轻松制作自己的看门狗。例如,您可以让您的程序定期编写一些临时文件,然后启动一个脚本,该脚本不时检查该文件,如果它有一段时间没有更新,则重新启动您的进程。

回答by darren102

Use /etc/inittabyou can utilize it to start in the specific run levels and if it is killed it shall be restarted automatically

使用/etc/inittab您可以利用它在特定的运行级别启动,如果它被杀死,它将自动重新启动

n:2345:respawn:/path/to/app

This will make it respawn in run levels 2345 you probably only need 3 and 5 but this will work fine and is built into Linux.

这将使它在运行级别 2345 中重新生成,您可能只需要 3 和 5,但这将正常工作并且已内置到 Linux 中。

回答by nope

Since the moderators ignore post-improvements now i'll have to post it seperately

由于版主现在忽略了后期改进,我将不得不单独发布

The linux software watchdog will reboot the machine, not just restart your process.

linux 软件看门狗将重新启动机器,而不仅仅是重新启动您的进程。

Well this is simply not true, it is very possible to restart single or multiple processes after the watchdog signals that the systems is hanging - you can even ABORT the reboot or do a SOFT-reboot, one is able to configure "test" and "repair"-scripts / binaries which do whatever you want them to do. The busybox-version of watchdog is stripped down to a near-unusable level ... i guess the world will never know why the busybox-devs decided to abandon primary functionalities - for now, it would be best to avoid busybox at all --> the speed-improvements are nearly inexistent, the size-decrease does not compensate the huge loss of functionality. /bin/bash is rather small - recompile everything with the flag "-Os" if size matters and you're good to go - an out-of-the-box watchdog which allows for just about everything one could want.

好吧,这根本不是真的,很可能在看门狗发出系统挂起的信号后重新启动单个或多个进程 - 您甚至可以中止重新启动或进行软重新启动,可以配置“测试”和“修复”-脚本/二进制文件,可以执行您希望它们执行的任何操作。看门狗的busybox 版本被精简到几乎无法使用的程度……我想全世界永远不会知道为什么busybox 开发者决定放弃主要功能——现在,最好完全避免busybox—— > 速度改进几乎不存在,尺寸减小并不能弥补功能的巨大损失。/bin/bash 相当小 - 如果大小很重要并且你'

Oh and PLEASE do NOT create your own watchdog - that'll most likely leave you with unhandled errors and make your life bad one day.

哦,请不要创建自己的看门狗 - 这很可能会给您留下未处理的错误,并且有一天会让您的生活变得糟糕。

回答by fabatera

If you are using systemd there are 2 watchdogs: one for hardware (using systemd.conf or using a watchdog daemon) and one for daemons initialized as services. If systemd is your option have a look at the following: http://0pointer.de/blog/projects/watchdog.html

如果您使用 systemd,则有 2 个看门狗:一个用于硬件(使用 systemd.conf 或使用看门狗守护程序),另一个用于初始化为服务的守护程序。如果 systemd 是您的选择,请查看以下内容:http: //0pointer.de/blog/projects/watchdog.html

回答by Sridhar Sarnobat

If anyone has arrived at this page looking for an operating system watchdog (which is not directly what the OP wanted), this is what you need:

如果有人到达此页面寻找操作系统看门狗(这不是 OP 直接想要的),这就是您所需要的:

sudo apt-get install watchdog
service watchdog status
service watchdog start 

To check that it's working execute:

要检查它是否正常工作,请执行:

tail -f /var/log/syslog | grep watchdog

You should see something like:

你应该看到类似的东西:

Jul 25 22:03:35 nuc watchdog[14229]: still alive after 733 interval(s)
Jul 25 22:03:36 nuc watchdog[14229]: still alive after 734 interval(s)
Jul 25 22:03:36 nuc watchdog[14229]: still alive after 735 interval(s)
Jul 25 22:03:37 nuc watchdog[14229]: still alive after 736 interval(s)
Jul 25 22:03:37 nuc watchdog[14229]: still alive after 737 interval(s)

I hope I'm answering the question correctly. All the other answers seem to be very different.

我希望我能正确回答问题。所有其他答案似乎都非常不同。

回答by zepher

You can try wdog which is a utility written in c++ and linking against the Kahless_9 framework. The source code for this can be downloaded from: https://github.com/zepher999/wdogand consequently updated to suite your own needs. There is still some TODO list changes required for the future but as is this should cater for your current requirements.

您可以尝试使用 c++ 编写的实用程序 wdog,并链接到 Kahless_9 框架。可以从以下位置下载源代码:https: //github.com/zepher999/wdog并随后更新以满足您自己的需求。未来仍然需要对 TODO 列表进行一些更改,但这应该可以满足您当前的需求。

The utility requires a csv file as input wherein all processes to be watched with their arguments are contained. Upon startup, the utility starts all of these processes designated in the csv file and monitors them for exit/termination whereupon it restarts the process.

该实用程序需要一个 csv 文件作为输入,其中包含要使用其参数监视的所有进程。启动时,该实用程序会启动 csv 文件中指定的所有这些进程,并监视它们是否退出/终止,然后重新启动进程。

Currently wdog allows for the stopping/killing of monitored processes as well as the ability to startup the utility in hot or cold mode. Hot mode allows the utility to use cached records to monitor already started processes while cold mode starting discards such cached values thereby attempting to start all processes.

目前 wdog 允许停止/终止受监视的进程以及在热模式或冷模式下启动实用程序的能力。热模式允许实用程序使用缓存记录来监视已启动的进程,而冷模式启动会丢弃此类缓存值,从而尝试启动所有进程。

The utility also has the ability to launch an instance of itself to monitor itself, thereby having a watchdog for the watchdog.

该实用程序还具有启动自身实例以监控自身的能力,从而为看门狗设置看门狗。

回答by Akki

You can use "Monit" utility to restart and monitor your services. Simply install by issuing command `"apt-get install monit".

您可以使用“Monit”实用程序来重新启动和监控您的服务。只需通过发出命令“apt-get install monit”进行安装。