守护服务

optimize
wanghaomei 1 year ago
parent 4ee5717b0d
commit 46305c447f

@ -1,7 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<configuration> <configuration>
<appSettings> <appSettings>
<add key="DeamonServiceName" value="ServiceDeamon"/>
<add key="DeamonServiceDisplayName" value="进程守护服务"/>
<add key="ServiceName" value="nginx"/> <add key="ServiceName" value="nginx"/>
<add key="CheckInteval" value="5000"/> <add key="CheckInteval" value="5000"/>
</appSettings> </appSettings>
</configuration> </configuration>

@ -10,25 +10,27 @@ namespace ServiceDeamon
{ {
public class CheckServiceHelper public class CheckServiceHelper
{ {
private static ServiceController servCtl;
private static string ServName = ConfigurationManager.AppSettings["ServiceName"]; private static string ServName = ConfigurationManager.AppSettings["ServiceName"];
private static ServiceControllerStatus LastStatus;
public static void CheckStatus() public static void CheckStatus()
{ {
if (servCtl == null || servCtl.ServiceName != ServName) var servCtl = new ServiceController(ServName);
if (LastStatus != servCtl.Status)
{ {
servCtl = new ServiceController(ServName); Console.WriteLine($"服务 {ServName} 的当前状态:{servCtl.Status}");
} }
Console.WriteLine($"服务 {ServName} 的当前状态:{servCtl.Status}");
if (servCtl != null && servCtl.Status == ServiceControllerStatus.Stopped) if (servCtl != null && servCtl.Status == ServiceControllerStatus.Stopped)
{ {
Console.WriteLine($"服务已停止,自动启动:{ServName}"); Console.WriteLine($"服务已停止,自动启动:{ServName}");
servCtl.Start(); servCtl.Start();
} }
LastStatus = servCtl.Status;
//Console.WriteLine("CheckStatus"); //Console.WriteLine("CheckStatus");
} }
} }

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
namespace ServiceDeamon
{
public class DeamonService : ServiceControl
{
private static Timer timer;
private static int CheckInteval = Convert.ToInt32(ConfigurationManager.AppSettings["CheckInteval"]);
public bool Start(HostControl hostControl)
{
timer = new Timer(new TimerCallback(CheckSta));
timer.Change(CheckInteval, Timeout.Infinite);
return true;
}
public bool Stop(HostControl hostControl)
{
timer.Change(-1, 0);
return true;
}
private static void CheckSta(object sta)
{
CheckServiceHelper.CheckStatus();
timer.Change(CheckInteval, Timeout.Infinite);
}
}
}

@ -1,27 +1,52 @@
using ServiceDeamon; using ServiceDeamon;
using System.Configuration; using System.Configuration;
using System.ServiceProcess; using System.ServiceProcess;
using Topshelf;
public class Program public class Program
{ {
private static Timer timer;
private static int CheckInteval = Convert.ToInt32(ConfigurationManager.AppSettings["CheckInteval"]);
public static void Main(string[] args) public static void Main(string[] args)
{ {
timer = new Timer(new TimerCallback(CheckSta)); try
timer.Change(CheckInteval, Timeout.Infinite); {
var serviceName = ConfigurationManager.AppSettings["DeamonServiceName"];
var serviceDisplayName = ConfigurationManager.AppSettings["DeamonServiceDisplayName"];
Console.Read();
} if (Environment.UserInteractive)
{
private static void CheckSta(object sta) Console.Title = serviceDisplayName;
{ }
CheckServiceHelper.CheckStatus();
Host host = HostFactory.New(x =>
timer.Change(CheckInteval, Timeout.Infinite); {
// 基本的配置
x.RunAsLocalSystem();
x.SetServiceName(serviceName);
x.SetDisplayName(serviceDisplayName);
x.StartAutomaticallyDelayed();
x.EnableShutdown();
// 注册服务
x.Service(hostSettings => new DeamonService());
// 设置服务失败后的操作,分别对应第一次、第二次、后续
x.EnableServiceRecovery(t =>
{
t.RestartService(0);
t.RestartService(0);
t.RestartService(0);
t.OnCrashOnly();
});
});
host.Run();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
} }
} }

Loading…
Cancel
Save