wet 1 year ago
commit e1ea1abee7

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ServiceName" value=""/>
<add key="ServiceName" value="nginx"/>
<add key="CheckInteval" value="5000"/>
</appSettings>
</configuration>

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceProcess;
using System.Text;
@ -11,18 +12,24 @@ namespace ServiceDeamon
{
private static ServiceController servCtl;
private static string ServName = ConfigurationManager.AppSettings["ServiceName"];
public static void CheckStatus(string servName)
public static void CheckStatus()
{
if (servCtl == null || servCtl.ServiceName != servName)
if (servCtl == null || servCtl.ServiceName != ServName)
{
servCtl = new ServiceController(servName);
servCtl = new ServiceController(ServName);
}
Console.WriteLine($"服务 {ServName} 的当前状态:{servCtl.Status}");
if (servCtl != null && servCtl.Status == ServiceControllerStatus.Stopped)
{
Console.WriteLine($"服务已停止,自动启动:{ServName}");
servCtl.Start();
}
//Console.WriteLine("CheckStatus");
}
}
}

@ -2,12 +2,26 @@
using System.Configuration;
using System.ServiceProcess;
var servName = ConfigurationManager.AppSettings["ServiceName"];
public class Program
{
private static Timer timer;
//Timer t = new System.Threading.Timer((sta) =>
//{
// CheckServiceHelper.CheckStatus(servName);
//});
private static int CheckInteval = Convert.ToInt32(ConfigurationManager.AppSettings["CheckInteval"]);
//t.Change(1000,)
public static void Main(string[] args)
{
timer = new Timer(new TimerCallback(CheckSta));
timer.Change(CheckInteval, Timeout.Infinite);
Console.Read();
}
private static void CheckSta(object sta)
{
CheckServiceHelper.CheckStatus();
timer.Change(CheckInteval, Timeout.Infinite);
}
}

@ -8,7 +8,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="7.0.1" />
<PackageReference Include="Topshelf" Version="4.3.0" />
</ItemGroup>
</Project>

Loading…
Cancel
Save