[C#]通用守护进程服务
摘要
很多情况下,都会使用windows服务做一些任务,但总会有一些异常,导致服务停止。这个时候,开发人员又不能立马解决问题,所以做一个守护者服务还是很有必要的。当检测到服务停止了,重启一下服务,等开发人员到位了,再排查错误日志。
代码
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<!--要守护的服务的服务名称-->
<add key="toWatchServiceName" value=""/>
<!--守护服务的名称-->
<add key="serviceName" value="邮件提醒服务守护服务"/>
<!--每1分钟检查一次 以秒为单位-->
<add key="timerInterval" value=""/>
</appSettings>
</configuration>
服务
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace WindowsService.Watch
{
partial class ServiceWather : ServiceBase
{
private static string currentExePath = string.Empty;
public ServiceWather()
{
InitializeComponent();
currentExePath = AppDomain.CurrentDomain.BaseDirectory;
}
/// <summary>
/// 检查间隔
/// </summary>
private static readonly int _timerInterval = Convert.ToInt32(ConfigurationManager.AppSettings["timerInterval"]) * ;
/// <summary>
/// 要守护的服务名
/// </summary>
private static readonly string toWatchServiceName = ConfigurationManager.AppSettings["toWatchServiceName"];
private System.Timers.Timer _timer;
protected override void OnStart(string[] args)
{
//服务启动时开启定时器
_timer = new System.Timers.Timer();
_timer.Interval = _timerInterval;
_timer.Enabled = true;
_timer.AutoReset = true;
_timer.Elapsed += _timer_Elapsed;
LogHelper.WriteLog(currentExePath, "守护服务开启");
} void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//如果服务状态为停止,则重新启动服务
if (!CheckSericeStart(toWatchServiceName))
{
StartService(toWatchServiceName);
}
} protected override void OnStop()
{
if (_timer != null)
{
_timer.Stop();
_timer.Dispose();
LogHelper.WriteLog(currentExePath, "守护服务停止");
}
}
/// <summary>
/// 启动服务
/// </summary>
/// <param name="serviceName">要启动的服务名称</param>
private void StartService(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName.Trim() == serviceName.Trim())
{
service.Start();
//直到服务启动
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(, , ));
LogHelper.WriteLog(currentExePath, string.Format("启动服务:{0}", serviceName));
}
}
}
catch (Exception ex)
{
LogHelper.WriteLog(currentExePath, ex);
}
}
private bool CheckSericeStart(string serviceName)
{
bool result = true;
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName.Trim() == serviceName.Trim())
{
if ((service.Status == ServiceControllerStatus.Stopped)
|| (service.Status == ServiceControllerStatus.StopPending))
{
result = false;
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteLog(currentExePath, ex);
}
return result;
}
/// <summary>
/// 停止
/// </summary>
/// <param name="serviceName"></param>
private void StopService(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName.Trim() == serviceName.Trim())
{
service.Stop();
//直到服务停止
service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(, , ));
LogHelper.WriteLog(currentExePath, string.Format("启动服务:{0}", serviceName));
}
}
}
catch (Exception ex)
{
LogHelper.WriteLog(currentExePath, ex);
}
}
}
}
[C#]通用守护进程服务的更多相关文章
- 在C#/.NET应用程序开发中创建一个基于Topshelf的应用程序守护进程(服务)
本文首发于:码友网--一个专注.NET/.NET Core开发的编程爱好者社区. 文章目录 C#/.NET基于Topshelf创建Windows服务的系列文章目录: C#/.NET基于Topshelf ...
- 用C#实现通用守护进程
1. 下载 源码下载:http://pan.baidu.com/s/1vqDA2 安装包下载:http://pan.baidu.com/s/1sjmEB0p 2. 安装注意事项 在配置档中配置你要守护 ...
- 创建Android守护进程(底层服务)【转】
本文转载自:https://blog.csdn.net/myfriend0/article/details/80016739 创建Android守护进程(底层服务) 前言 Android底层服务,即运 ...
- Linux_控制服务与守护进程
一.systemd 1.systemd简介 1️⃣:systemd是用户空间的第一个应用程序,即/sbin/init 2️⃣:init程序的类型: SysV风格:init(centos5),实现系统初 ...
- 守护进程与Supervisor
博客链接:http://www.cnblogs.com/zhenghongxin/p/8676565.html 消息队列处理后台任务带来的问题 在系统稍微大些的时候,我们经常会用到消息队列(实现的方式 ...
- shell脚本(管理守护进程)
工作中常常会遇到处理消息队列的消费者进程,这样的进程是一个守护进程,即一个服务.服务通常写个shell脚本来管理,查询服务的status ,启动start 关闭stop 重启reload.最近在学 ...
- CentOS7 安装supervisor守护进程管理器
supervisor没有发布在标准的CentOS源在,需要安装epel源.这种方式安装的可能不是最新版本,但比较方便,安装完成之后,配置文件会自动帮你生成. 默认配置文件:/etc/superviso ...
- asp.net core2.0 部署centos7/linux系统 --守护进程supervisor(二)
原文:asp.net core2.0 部署centos7/linux系统 --守护进程supervisor(二) 续上一篇文章:asp.net core2.0 部署centos7/linux系统 -- ...
- supervisord守护进程的使用
原文链接:http://blog.csdn.net/xyang81/article/details/51555473 Supervisor(http://supervisord.org/)是用Pyth ...
随机推荐
- [vijos1907][NOIP2014]飞扬的小鸟
Description 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便宣告失败. ...
- css-css权威指南学习笔记2
第二章 选择器 1.在font属性中,只有一种情况下可以使用/来分隔两个特定的关键字,即元素的字体大小/行高. 2.通过把两个类选择器链接在一起,可以选择同时包含这些类名的元素,类名的顺序不限. .w ...
- 在Eclipse中如何关联spring-framework的文档和源代码
1.到官方网站去下载spring-framework的jar包 spring-framework jar包的下载地址是:http://repo.spring.io/release/org/spring ...
- springMVC-数据绑定流程
1.Spring MVC主框架将ServletRequest对象以及目标方法的入参实例传递给WebDataBinderFactory实例,以创建DataBinder(数据绑定器)实例对象 2.Data ...
- Windows 8及以上系统安装好SQL Server 2008之后找不到SQL Server配置管理器的问题
直接的方法: 打开[运行]->输入[C:\Windows\SysWOW64\mmc.exe /32 C:\Windows\SysWOW64\SQLServerManager10.msc]即可. ...
- 洛谷P1808 单词分类
题目描述 Oliver为了学好英语决定苦背单词,但很快他发现要直接记住杂乱无章的单词非常困难,他决定对单词进行分类. 两个单词可以分为一类当且仅当组成这两个单词的各个字母的数量均相等. 例如“AABA ...
- 谈谈favicon和他带来的问题
favicon.ico介绍 favicon.ico是个什么东西呢,也许见得太多都习以为常了(我就是这样,直到写这篇文章之前才知道),看看维基百科的解释: Favicon是favorites icon的 ...
- linux 下各文件夹的功能性介绍。(转载)
原文来自:http://www.cnblogs.com/wen858636827/archive/2012/12/26/2834373.html /opt 放置用户自己下载的软件 英文全称是op ...
- JS方法集
数组方法集 Angela.array = { //# 数组方法 // index, 返回位置! 不存在则返回 -1: index: function (t, arr) { //# 返回当前值所在数组的 ...
- jquery 图片浏览功能实现
效果展示: HTML代码: <div id="no3"> <img src="./img/last.png" id="last&qu ...