3

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

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."

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.

I really appreciate your help.

Community
  • 1
  • 1
  • Who is John? You don't mean Jon by any chance? – Ash Sep 23 '09 at 02:55
  • That would be right: go straight to the top. Don't bother asking the rest of us *dummies*. – pavium Sep 23 '09 at 03:16
  • Actually it was John Dyer - see my update to the question. – paxdiablo Sep 23 '09 at 04:03
  • Right, now perhaps that the question has been Anglicized a bit, we could stop downvoting it and start answering it :-) – paxdiablo Sep 23 '09 at 04:07
  • This does *not* belong on superuser. It's specifically asking how to code a watchdog process. – paxdiablo Sep 23 '09 at 04:08
  • Aww man now the first comment makes no sense! :( – RCIX Sep 23 '09 at 04:11
  • S'ok, @RCIX, people will look at the edit history. And did you know you sound *exactly* like Swiper from Dora the Explorer ("Aww, man"). That'll only make sense to parents with kids in the range 2-5yo, and possibly some very strange adults without kids, you know, those that have downgraded from Manga to something even *more* childish :-) – paxdiablo Sep 23 '09 at 04:46
  • I had absolutely no intention of sounding that way, i don't even watch the show! though may i ask, how od YOU know that? ;) – RCIX Sep 23 '09 at 04:55
  • Thanks, Pax. I will try your suggestion. Thanks ArsenMkrt, I will also try your suggestion to set the service property. --Jeff –  Sep 23 '09 at 16:19
  • I am going to throw this here and hope it finds the next reader well: [Code Project: Watchdog Service](http://www.codeproject.com/KB/security/WatchDog.aspx) – abutbul Jul 05 '11 at 11:56
  • http://stackoverflow.com/questions/220382/how-can-a-windows-service-programmatically-restart-itself This might be a better solution – Hoang Nguyen Apr 17 '13 at 18:04

3 Answers3

6

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 very simple 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).

Then have your watched program periodically rewrite that file.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
4

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.

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.

3

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 
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • 4
    Might work for some services, but what about a service that usually always runs but suddenly gets stuck and stops making progress? – Roman Starkov Apr 20 '10 at 10:27