检测Windows服务失败

Mil*_*had 3 windows service status detection

一个Windows服务是否可以检测其他正在运行的Windows服务之一是否已停止?

出于某种原因,第三方服务有时会停止,我必须再次手动启动它们.我需要一项服务来自动化这个烦人的过程.

Gra*_*mas 5

我强烈建议你针对纠正被诊断的问题而不是消防症状的目标.找出为什么第三方服务停止/失败,防患于未然的问题.

但是,如果您必须实现暂时的东西,看一看ServiceController,与找到的信息这个MSDN链接.这将允许您查询服务及其状态,并进一步控制它们,如Start根据需要调用您的特定情况.

假设(可能是更好的判断)你使用.NET,并且由于我对它的亲和力,C#作为语言,请考虑以下快速的袖手旁观:

//Add a reference to System.ServiceProcess

using System.ServiceProcess;

var services = ServiceController.GetServices();
foreach (var service in services)
{
    if (service.ServiceName == myServiceName &&
        service.Status == ServiceControllerStatus.Stopped)
    {
        service.Start();
    }
}
Run Code Online (Sandbox Code Playgroud)