6

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?

Kiquenet
  • 13,271
  • 31
  • 133
  • 232
user280154
  • 91
  • 1
  • 1
  • 3

4 Answers4

16

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
  • 7,982
  • 2
  • 47
  • 60
  • 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 ".". – user280154 Feb 24 '10 at 08:44
  • @user: Ow, but that's an other problem then. The event log should show more details about why the service can't start. – fretje Feb 24 '10 at 08:46
  • 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. – user280154 Feb 24 '10 at 08:50
  • 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. – user280154 Feb 24 '10 at 08:52
  • @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 Feb 24 '10 at 09:03
  • 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 Feb 24 '10 at 10:34
  • @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 Feb 24 '10 at 11:00
  • 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 Feb 24 '10 at 11:09
11

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.

Rob van Groenewoud
  • 1,675
  • 1
  • 12
  • 17
2

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

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

Oded
  • 463,167
  • 92
  • 837
  • 979
  • 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 Feb 24 '10 at 08:48
  • @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 Feb 24 '10 at 08:56
1

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.

Kiquenet
  • 13,271
  • 31
  • 133
  • 232
CoreTech
  • 2,187
  • 2
  • 16
  • 23