How to monitor a Windows process and restart it when it goes down?

How to monitor a Windows process and restart it when it goes down?

Q: Certain processes on my Windows computer should always be running. How can I detect that a process no longer runs and restart it?

A: You can use WMI Process monitor to check if the process in question is alive: set the ‘Name’ monitor parameter to the process executable name, like ‘startup.exe’ and leave the ‘Metric’ parameter as default ‘Processes total’. The monitor will show the number of running processes with the specified name, so if the number is ‘0’, than the process is stopped and should be restarted.

WMI process monitor setup

Also, you can use a custom WMI query to find if the process is alive. Note that each monitor should return integer value, so you should SELECT some integer property of the process to use it as a monitor performance value. You can choose any integer property of Win32_Process class, e.g. ProcessId, HandleCount or VirtualSize from the list specified at:
http://msdn.microsoft.com/en-us/library/aa394372(VS.85).aspx

For example, you can use the following query to find if the process is alive:
SELECT ProcessId FROM Win32_Process WHERE Name = ‘startup.exe’

Windows process monitor setup

To restart the program you can use 2 options. Both use an alert configured to run once a monitor switches to Down state:

  1. If you need to restart the program on the local host the IPHost is installed on, you can use the “Execute Program” alert in the “Run program” mode.
  2. If you need to restart the program on a remote server you should use an external VBS script. Put it on the local host and use the “Execute Program” alert in “Run Script” mode to run the script remotely.

The following simple VBS script can be used to restart a process:

Restart.vbs file:
----------------------
Dim args
Set args = WScript.Arguments

strComputer = args(0)
strCommandLine = args(1)

Set objWMIService = GetObject _ 
    ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process") 
  
Error = objWMIService.Create(strCommandLine, null, null, intProcessID) 

WScript.quit Error
----------------------

The script accepts 2 parameters: server host name and the process command line. It can be started from command prompt as:

    cscript restart.vbs server C:\\somedirectory\\startup.exe

To call the script from IPHost Network Monitor you should create a custom alerting rule and alert for the monitor and configure them as shown on the screenshots below:

Restart Program Alert setup

Alerting rule

For more details on how to configure alerts please refer to online help topic on Alerting and actions.

Related Topics:
Monitoring Remote Servers Through Firewalls
Software application monitoring