Creating a Watch Dog Application

Wednesday Nov 27th 2002 by Tali Gat-Falik

This code implements a watch dog (a simple program that has to keep another program alive). It can check to see whether an application is running or if it has hung. It can also check how long the application has been running and restart it if necessary.

Environment: C++, Frameworks

The accompanying code takes care of another application (call it "myApp") that should always be running.

It wakes up every X times, and checks the following:

  1. Is myApp running? If not, it turns it on.
  2. Is myApp hanging? (This is decided by myApp not consuming any CPU usage during the last X times and since the previous check.) If yes, kills it and restarts it.
  3. Is myApp already running for Y time? If yes, kills it and restarts it.

The program keeps a log file of its actions (checking, restarting, killing). It also copies the log file of myApp (if any) to another location (same name, plus date and time concatenated), so that the information in it will not get lost.

This code was compiled under VC++ 6.0. It needs the following definitions:

  • _WIN32
  • WIN32
  • _DEBUG
  • _MBCS


Download source - 3 Kb
Mobile Site | Full Site