如何编写看门狗计时器以重新启动 Windows 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1463689/
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-09-15 13:11:12  来源:igfitidea点击:

How do I code a watchdog timer to restart a Windows service?

windowsservicerestartwatchdog

提问by paxdiablo

I'm very interested in the answer to another question regarding watchdog timers for Windows services (see here). That answer stated:

我对关于 Windows 服务的看门狗定时器的另一个问题的答案非常感兴趣(请参阅此处)。那个回答说:

I have also used an internal watchdog system running in another thread. That thread looks at the main thread for activity like log output or a toggling event. If the activity is not seen then the service is considered hung and I shutdown the service.

In this case you can configure windows to auto-restart a stopped service and that might clear the problem (as long as it's not an internal logic bug).

Also services I work with have text logs that are written to a log. In addition for services that are about to "sleep for a bit", I log the time for the next wake up. I use MTAIL to watch a log for output."

我还使用了在另一个线程中运行的内部看门狗系统。该线程查看主线程中的日志输出或切换事件等活动。如果未看到该活动,则认为该服务已挂起,我将关闭该服务。

在这种情况下,您可以将 Windows 配置为自动重新启动已停止的服务,这可能会解决问题(只要它不是内部逻辑错误)。

我使用的服务也有写入日志的文本日志。另外对于即将“睡一会儿”的服务,我会记录下一次唤醒的时间。我使用 MTAIL 来观察输出日志。”

Could anyone give some sample code how to use an internal watchdog running in another thread, since I currently have a task to develop a windows service which will be able to self restart in case it failed, hung up, etc.

任何人都可以提供一些示例代码,如何使用在另一个线程中运行的内部看门狗,因为我目前有一项任务来开发 Windows 服务,该服务将能够在失败、挂断等情况下自行重启。

I really appreciate your help.

我真的很感谢你的帮助。

回答by Arsen Mkrtchyan

You can configure from service properties to self restart in case of failure

您可以从服务属性配置为出现故障时自重启

Services -> right-click your service -> Properties -> First failure : restart the service -> Second failure : restart the service -> Subsequent failure : restart 

回答by paxdiablo

I'm not a big fan of running a watchdog as a thread in the process you're watching. That means if the whole process hangs for some reason, the watchdog won't work.

我不喜欢在您正在观看的过程中将看门狗作为线程运行。这意味着如果整个过程由于某种原因挂起,看门狗将无法工作。

Watchdogs are an idea lifted from the hardware world and they had it right. Use an external circuit as simple as possible (so it can be provably correct). Typical watchdogs simply ran an timer and, if the process hadn't done something before the timer expired (like access a memory location the watchdog was watching), the whole thing was reset. When the watchdog was "kicked", it would restart the timer.

看门狗是从硬件世界中提出的一个想法,他们做对了。使用尽可能简单的外部电路(因此可以证明它是正确的)。典型的看门狗只是运行一个计时器,如果进程在计时器到期之前没有做任何事情(比如访问看门狗正在监视的内存位置),整个事情就会被重置。当看门狗被“踢”时,它会重新启动定时器。

The act of the process kicking the watchdog protected that process from summary termination.

进程踢看门狗的行为保护了该进程免于立即终止。

My advice would be to write a verysimple stand-alone program which just monitored an event (such as file update time being modified). If that event didn't occur within the required time, kill the process being watched (and let Windows restart it).

我的建议是编写一个非常简单的独立程序,它只监视一个事件(例如文件更新时间被修改)。如果该事件未在要求的时间内发生,则终止正在监视的进程(并让 Windows 重新启动它)。

Then have your watched program periodically rewrite that file.

然后让您观看的程序定期重写该文件。

回答by Tom Kirby-Green

Other approaches you might want to consider besides regularly modifying the lastwritetime of a file would be to create a proper performance counter or even a WMI object. We do the later in our build infrastructure, the 'trick' is to find a meaningful work unit in the service being monitored and pulse your 'heartbeat' each time a unit is finished.

除了定期修改文件的 lastwritetime 之外,您可能想要考虑的其他方法是创建适当的性能计数器甚至 WMI 对象。我们在构建基础设施中做后面的工作,“技巧”是在被监控的服务中找到一个有意义的工作单元,并在每次完成一个单元时触发你的“心跳”。

The advantage of WMI or Perf Counters over a the file approach is that you then become visible to a whole bunch of professional MIS / management tools. This can add a lot of value.

WMI 或 Perf Counters 相对于文件方法的优势在于,您可以看到一大堆专业的 MIS/管理工具。这可以增加很多价值。