How to encode a watchdog to restart a Windows service?

I am very interested in the answer to another question about watchdog timers for Windows services (see here ). This answer stated:

I also used an internal watchdog system running on a different thread. This thread considers the main thread for activity, such as a log output or a switch event. If the activity is not visible, the service is considered to be hung, and I end the service.

In this case, you can configure windows to automatically restart a stopped service and fix the problem (if this is not an internal logic error).

Also, the services I work with have text logs that are written to the log. In addition, for services that are going to "get some sleep," I am recording the time for the next wake up. I use MTAIL to view the log for output. "

Can someone give an example of code on how to use an internal watchdog running on a different thread, as I currently have a task to develop a Windows service that can reboot itself if it fails, hang up, etc.

I really appreciate your help.

+4
source share
3 answers

I'm not a big fan of running a watchdog as a thread in the process you are watching. This means that for some reason the whole process freezes, the watchdog timer will not work.

Watchdogs is an idea taken from the hardware world, and they all worked out. Use an external circuit as simple as possible (so it can be convincingly correct). Typical watchdogs simply started the timer, and if the process did nothing before the timer expired (for example, accessing the memory where the watchdog was watching), it was all reset. When the watchdog has been kicked, it will restart the timer.

The act of starting the watchdog timer protected this process from final completion.

My advice would be to write a very simple standalone program that just kept track of the event (e.g. file update time). If this event does not occur within the required time, kill the observed process (and let Windows restart it).

Then your monitored program periodically overwrites this file.

+5
source

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

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

Other approaches that you might want to consider, in addition to regularly modifying the file’s last time, would be to create an appropriate performance counter or even a WMI object. We do later in our build infrastructure, the β€œtrick” is to find a meaningful work unit in the service you are servicing and to pulse your heartbeat every time the device is completed.

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

+3
source

All Articles