[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 ...
随机推荐
- iOS黑客技术相关
在黑客的世界里,没有坚不可破的防护系统,也没有无往不胜.所向披靡的入侵利器,有时候看似简单的问题,破解起来也许花上好几天.好几个月,有时候看似很 low 的工具往往能解决大问题:我们以实现微信自动抢红 ...
- 3.python算法之完全数
代码: #!/usr/bin/env python # encoding: utf-8 """ @author: 侠之大者kamil @file: 3.完全数.py @t ...
- Oracle数据库安装及配置(一)
http://blog.csdn.net/phacker2010/article/details/7190126 1.安装 Oracle 版本:Oracle Database 10g Release ...
- bzoj3295: [Cqoi2011]动态逆序对(树套树)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- namespace std
c++中使用namespace来防止命名冲突(重命名),我们经常使用的一些函数和变量都被放在一个叫std的namespace中,如标准I/O流操作,vector等等.我们在每一个文件中都可使用std中 ...
- 获取exe目录
System.IO.Directory.GetCurrentDirectory()这个方法,会随着你的当前系统路径的改变而改变.比如你打开一个openFileDialog那么,再次获得路径就不对了.s ...
- SQL Server 分页
select top "+pageSize+" * from Table where id not in (select top "+(currentPage - 1) ...
- 大一上学期C语言学习心得总结
经过一个学期的C语言学习,大体算是在这个编程语言上入了门,能够通过一些代码解决特定的问题.当然,每次成功将问题转换成代码都小有激动,虽然只是在黑框上输出了一些数字或是字符串. 编程,虽然还不是很懂,但 ...
- Java中StringBuilder的清空方法比较
StringBuilder 没有提供clear或empty方法.清空有3种方法:1)使用delete2)新生成一个,旧的由系统自动回收3)使用setLength 将三种方法循环1000万次,代码: p ...
- RNN 入门教程 Part 1 – RNN 简介
转载 - Recurrent Neural Networks Tutorial, Part 1 – Introduction to RNNs Recurrent Neural Networks (RN ...