C# 如果windows服务停止,请发送电子邮件给我
我希望能够检查某个服务是否正在运行(比如它有一个显示名-ServiceA)。我希望我的程序每5分钟检查一次,确认服务仍在运行。如果正常,它将循环并再等待5分钟,然后再次检查。如果发现ServiceA已停止,我希望程序通过电子邮件告诉我……ServiceA已停止运行。下面是我到目前为止所做的代码,它能够将当前运行的所有服务以及实际的显示名称拉回到控制台。有人对我上面需要的代码/逻辑有什么想法吗C# 如果windows服务停止,请发送电子邮件给我,c#,.net,windows,windows-services,C#,.net,Windows,Windows Services,我希望能够检查某个服务是否正在运行(比如它有一个显示名-ServiceA)。我希望我的程序每5分钟检查一次,确认服务仍在运行。如果正常,它将循环并再等待5分钟,然后再次检查。如果发现ServiceA已停止,我希望程序通过电子邮件告诉我……ServiceA已停止运行。下面是我到目前为止所做的代码,它能够将当前运行的所有服务以及实际的显示名称拉回到控制台。有人对我上面需要的代码/逻辑有什么想法吗 namespace ServicesChecker { class Program {
namespace ServicesChecker
{
class Program
{
static void Main(string[] args)
{
ServiceController[] scServices;
scServices = ServiceController.GetServices();
Console.WriteLine("Services running on the local computer:");
foreach (ServiceController scTemp in scServices)
{
if (scTemp.Status == ServiceControllerStatus.Running)
{
Console.WriteLine();
Console.WriteLine(" Service : {0}", scTemp.ServiceName);
Console.WriteLine(" Display name: {0}", scTemp.DisplayName);
}
}
//Create a Pause....
Console.ReadLine();
}
}
}
将每个服务的名称放入一个数组中,并检查您想要的名称是否正在运行
List<string> arr = new List<string>();
foreach (ServiceController scTemp in scServices)
{
if (scTemp.Status == ServiceControllerStatus.Running)
{
arr.add(scTemp.ServiceName);
}
}
if (arr.Contains("YourWantedName")
{
// loop again
}
else
{
// send mail
}
List arr=new List();
foreach(scServices中的服务控制器scTemp)
{
if(scTemp.Status==ServiceControllerStatus.Running)
{
arr.add(scTemp.ServiceName);
}
}
如果(arr.Contains(“YourWantedName”)
{
//再次循环
}
其他的
{
//寄信
}
您需要跟踪需要某种存储的服务状态。最简单的可能是跟踪服务状态的XML文件,也可能是这样的模式
<services>
<service name="service1" last-check="12/21/2011 13:00:05" last-status="running" />
...
</services>
...
您的监控应用程序将唤醒,以查找其感兴趣的服务的状态,并检查该服务以前的状态。如果该状态正在运行,但当前已停止,请发送电子邮件。如果未找到该服务,请将其添加到服务列表中
将服务的状态持久保存到磁盘可以在监控应用程序关闭时保护您。以下是一个服务示例,其功能非常类似。应该很简单,以适应您的需要
public partial class CrowdCodeService : ServiceBase
{
private Timer stateTimer;
private TimerCallback timerDelegate;
AutoResetEvent autoEvent = new AutoResetEvent(false);
public CrowdCodeService()
{
InitializeComponent();
}
int secondsDefault = 30;
int secondsIncrementError = 30;
int secondesMaximum = 600;
int seconds;
protected override void OnStart(string[] args)
{
Loggy.Add("Starting CrowdCodeService.");
timerDelegate = new TimerCallback(DoSomething);
seconds = secondsDefault;
stateTimer = new Timer(timerDelegate, autoEvent, 0, seconds * 1000);
}
static bool isRunning = false;
// The state object is necessary for a TimerCallback.
public void DoSomething(object stateObject)
{
if (CrowdCodeService.isRunning)
{
return;
}
CrowdCodeService.isRunning = true;
AutoResetEvent autoEvent = (AutoResetEvent)stateObject;
try
{
////// Do your work here
string cs = "Application";
EventLog elog = new EventLog();
if (!EventLog.SourceExists(cs))
{
EventLog.CreateEventSource(cs, cs);
}
elog.Source = cs;
elog.EnableRaisingEvents = true;
elog.WriteEntry("CrowdCodes Service Error:" + cmd.Message.ToString(), EventLogEntryType.Error, 991);
}
}
finally
{
CrowdCodeService.isRunning = false;
}
}
protected override void OnStop()
{
Loggy.Add("Stopped CrowdCodeService.");
stateTimer.Dispose();
}
}
如果你知道你要找的是哪一个服务,就不需要迭代所有的服务:你可以 至于发送电子邮件:看一下课堂
注意:您知道,您也可以将服务配置为在失败时触发操作?当检查程序停止运行时会发生什么情况…!Windows已经可以为您这样做了。您不需要编写应用程序来触发服务停止或失败时的操作。@Cody:Hoes Windows会这样做吗?@abatishchev:在“服务”中manager(
services.msc
),右键单击服务,然后切换到“恢复”tab.您可以要求Windows在服务第一次、第二次和后续失败时执行各种操作,包括启动应用程序。没有理由编写轮询应用程序。连续轮询几乎总是错误的解决方案。@Cody-如果服务失败,是否可以将其配置为发送电子邮件?请参阅来自c#的结束邮件:它应该包含发送简单邮件所需的所有内容。请不要建议在2011年底使用非通用容器,如ArrayList
。改用通用容器,如List
。@abatishchev-循环代码的逻辑是什么,这样我就可以继续检查数组中的服务名称了?y您可以使用计时器,它需要每X分钟/小时调用一次此功能。对于计时器,请在本网站或msdn网站上搜索以使服务触发操作-这是否意味着您可以在它停止时触发它发送电子邮件?您如何执行此操作?查找(或编写)一种命令行工具,用于发送电子邮件,并按照Cody Gray在对您的问题的评论中描述的说明进行配置。