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

标签 windows service restart watchdog

我对另一个有关 Windows 服务看门狗计时器的问题的答案非常感兴趣(请参阅 here)。该答案指出:

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 服务,该服务将能够在失败、挂起等情况下自行重启。

非常感谢您的帮助。

最佳答案

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

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

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

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

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

关于windows - 如何编写看门狗定时器以重新启动 Windows 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1463689/

相关文章:

python - 为什么 os.path.exists() 会阻止 Windows 命名管道连接?

swift - 在 Finder 上下文中建立服务

php - 如何测试 ZF2 服务

python - 如何使用 Pygame 创建 "play again"选项

java - 如何重新启动一个线程

windows - 循环的批处理脚本不起作用

windows - 使用 PuTTY 连接到运行 freeSSHd 的 Amazon EC2 Windows 实例

windows - 为什么在 Windows Vista x64 上调用 GlobalMemoryStatus 时可用物理内存 (dwAvailPhys) > 可用虚拟内存 (dwAvailVirtual)

android - 无法使用此堆栈跟踪找到我崩溃的原因

java - 基于 Eclipse 的 E3 应用程序,切换工作区不起作用