views:

444

answers:

4

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?

+2  A: 

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.

fretje
thanks for the ans but this i have done but when it is stopped sc.start() gives an error like it cannot start the service on machine name ".".
@user: Ow, but that's an other problem then. The event log should show more details about why the service can't start.
fretje
see if the windows service is not running then this code will not work right so how to check whether the service is running or not for outside of this windows service.
that is true i have a event log to see but when i deploy it on user PC then i cant have any ans for that so i have made a custom log for this to log it in a log.txt.
@user: "how to check whether the service is running or not for outside of this windows service." Like already mentioned: just create another service which does the check, or create it as a console application, which can then be scheduled.
fretje
I still don't see the advantage in creating an additional process (service or console app) to keep a Windows Service alive. It sounds like digging a new hole to fill up the existing one.
Rob van Groenewoud
@Rob: Exactly, that's why I mentioned the OP should check the event log and fix the actual issue here in stead of using duct tape to fix it ;-)
fretje
Also: That's the way Google Update used to work. There was a service running continuously, and a scheduled task ran now and then to check if the service was still running. They (thankfully) changed that now to only run as a scheduled task (http://google-opensource.blogspot.com/2009/07/google-update-regularly-scheduled.html).
fretje
A: 

If the service is not running, it cannot check itself.

You will need to use a second service that does the checking.

Oded
right that was the ans i was looking for now my next question will be can i use a thread to do it if so then how can i make a thread out of the current service if you can share any site which explains it would be highly appreciated.
@user280154 - Please ask the threading question as a new question. And don't forget to _accept_ the answer (click the check mark next to the answer you like).
Oded
+3  A: 

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:

Service Recovery settings

Rob van Groenewoud
A: 

Hi.

The built-in Windows Services Recovery functionality (see the screenshot in Rob's post above) will probably meet your needs. If not, I suggest trying Service Protector, which is designed to automatically keep your important Windows Services running 24/7.

Good luck!

CoreTech