开发者

Check if a windows service is running? [duplicate]

开发者 https://www.devze.com 2022-12-20 22:12 出处:网络
This question already has answers here: How can I verify if a Windows Service is running (2 answers) Closed 8 years ago.
This question already has answers here: How can I verify if a Windows Service is running (2 answers) Closed 8 years ago.

I need to check whether my window service is running or not every 15 minutes o开发者_运维知识库r so.

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


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.


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.


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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消