Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a windows service is running? [duplicate]

I need to check whether my window service is running or not every 15 minutes or so.

If it is not running, then how can I restart the windows service again?

like image 598
user280154 Avatar asked Feb 24 '10 08:02

user280154


2 Answers

You can check if a service is running with a ServiceController:

ServiceController sc = new ServiceController("servicename");

if  ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
     (sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
   // Start the service if the current status is stopped.
   sc.Start();
}  

Of course, you will need to call this from another service, or create it as a small program which you then can schedule to run every 15 minutes or so.

like image 95
fretje Avatar answered Oct 01 '22 07:10

fretje


You don't need an extra process to recover your service:

If you want to be certain that your windows service is always running, check its properties in the Recovery tab. Set all failure actions to "Restart the Service" and set "Restart service after" to 0 minutes. The moment your service disappears it will be restarted immediately. Increase the timeout if it's ok to wait a bit longer before a restart is done.

like image 26
Rob van Groenewoud Avatar answered Oct 01 '22 07:10

Rob van Groenewoud