How to monitor Windows services

Viewed 99815

I'm looking for a way to monitor certain Windows services (on Windows Server 2003) and restart if necessary. The services are on different servers and include mainly SQL Server services (e.g. SQL Server Agent), but also some proprietary services.

Email alerts sent out if a service has stopped would be very useful as well.

6 Answers

A "might be enough" version of this is built into Windows. Look into the "Recovery" tab of the service properties, as available via services.msc.

You can act on a service fail with:

  • "Restart the Service"
  • "Run a Progam"
  • "Restart the Computer"

"Run a program" could be a small script that sends a mail, for example.

If you want a bigger solution with an overview dashboard and all, there are plenty of system monitoring solutions available. For example SolarWinds IPMonitor comes to mind, or Nagios, or Cacti.

If you are interested in some .NET programming, The System.ServiceProcess namespace provides classes that allow you to implement, install, and control Windows service applications.

Simple example, checking and starting a service in C#:

var srv = new ServiceController("MyService");
Console.WriteLine("MyService Status {0}", srv.Status);
if (srv.Status != ServiceControllerStatus.Running)
    srv.Start();

They may be some dedicated tools out there, but I just want to point out the wmic tool.

wmic /node:[hostname] service list

is able to list the services of any computer

WMIC SERVICE where caption='TELNET' CALL STARTSERVICE

would restart the telnet service.

If you encapsulate wmic in a script language (able to send email), you can have the monitoring tool you are looking for.

This depends on exactly what you want to monitor:

  • A service has actually stopped as far as the Service Control Manager (SCM) is concerned.
  • A service has crashed without the SCM being aware - this is very common due to threading.
  • A service has hung without the SCM being aware - also very common.

For the first item, you can configure the service to kick-off a script that sends an email alert. Note this can be really annoying if the service keeps rebooting due to circumstances outside its control (dependence on a flaky network connection, or whatever).

For the other two items, you will need some type of heartbeat service, which you can either build or buy. Be careful to have the heartbeat monitor running local to the services it's monitoring because as I wrote a while ago, the network is not reliable.

Related