C# 写的windows服务进程守护实现
最近在做一个windows服务实现windows socket server读取系统硬件,RFID的内容给WEB网站使用。在测试过程中,发现RFID读取过程有时会导致服务无端中止。因为涉及到硬件调用,查不到具体的原因。所以就简单的处理,做一个进程守护,当windows的服务进程退出之后,自动重新开启。以前使用的方法是,另外做一个程序监测。因为那种方法比较麻烦,现在介绍一种更简单的方法。使用cmd使用。
1. 首先建立复制一下内容。新建一个autostart.bat的脚本文件。
@echo off
rem 定义循环间隔时间和监测的服务:
set secs=60
set srvname="服务名称"
echo.
echo ========================================
echo == 查询计算机服务的状态, ==
echo == 每间隔%secs%秒种进行一次查询, ==
echo == 如发现其停止,则立即启动。 ==
echo ========================================
echo.
echo 此脚本监测的服务是:%srvname%
echo.
if %srvname%. == . goto end
:chkit
set svrst=0
for /F "tokens=1* delims= " %%a in ('net start') do if /I "%%a %%b" == %srvname% set svrst=1
if %svrst% == 0 net start %srvname%
set svrst=
rem 下面的命令用于延时,否则可能会导致cpu单个核心满载。
ping -n %secs% 127.0.0.1 > nul
goto chkit
:end
双击该批处理文件,运行界面如下
========================================
== 查询计算机服务的状态, ==
== 每间隔%secs%秒种进行一次查询, ==
== 如发现其停止,则立即启动。 ==
========================================
此脚本监测的服务是:%srvname%
如果%srvname%停止后,该批处理检测到后会重启该服务,界面如下
========================================
== 查询计算机服务的状态, ==
== 每间隔%secs%秒种进行一次查询, ==
== 如发现其停止,则立即启动。 ==
========================================
此脚本监测的服务是:%srvname%
%srvname% 服务正在启动
%srvname% 服务已经启动成功。
按实际情况修改
set srvname="服务名称"
这里的服务名称。
把文件保存到服务的安装文件夹。
2. C#相关代码
///
/// 启动服务
///
///
protected override void OnStart(string[] args)
{
StartTimer();
}
System.Timers.Timer timer = new System.Timers.Timer();
///
/// 启动定时器
///
private void StartTimer()
{
timer.Interval = 1000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
///执行的
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!PortInUse(13001))
{
//LogOuts.Debug("open:"+1);
//启动socket
StartWS();
}
else
{
//LogOuts.Debug("open:" + 2);
}
//如果没执行命令,执行cmd监听。
if (!execdCmd)
{
execdCmd = true;
execCmd();
}
}
private bool execdCmd = false;
private void execCmd()
{
Process proc = null;
try
{
proc = new Process();
proc.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory+ @"\\autostart.bat";
proc.StartInfo.Arguments = string.Format("");//this is argument
proc.StartInfo.CreateNoWindow = false;
proc.Start();
proc.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
}
}
https://github.com/users/bvaoih007/projects/1039
https://github.com/users/bvaoih007/projects/1039%3Ffullscreen%3Dtrue
https://www.github.com/users/bvaoih007/projects/1039
http://github.com/users/bvaoih007/projects/1039
https://github.com/users/bvaoih007/projects/1039?fullscreen=true
https://github.com/users/bvaoih007/projects/1039?I50zc=tzYkC
https://github.com/users/bvaoih007/projects/1039?zk4Bu=79
https://github.com/users/bvaoih007/projects/1039?zyqcl=cqrj
https://github.com/users/bvaoih007/projects/1039?zsqrc=83928
https://github.com/users/bvaoih007/projects/1039?36HRL/UQUj4=99
https://github.com/users/bvaoih007/projects/1039?URZ52/97546=59
https://github.com/users/bvaoih007/projects/1039?ubrkh/suty=23
https://github.com/users/bvaoih007/projects/1039?baahb/76049=38
https://github.com/users/bvaoih007/projects/1039/
https://github.com/users/bvaoih007/projects/1039?/#/tibbh=682Sl
https://github.com/users/bvaoih007/projects/1039/#/zAyS2=42
https://github.com/users/bvaoih007/projects/1039?ukira/#/ihhu=27
https://github.com/users/rl9322179/projects/1025
https://github.com/users/rl9322179/projects/1025%3Ffullscreen%3Dtrue
https://www.github.com/users/rl9322179/projects/1025
http://github.com/users/rl9322179/projects/1025
https://github.com/users/rl9322179/projects/1025?fullscreen=true
https://github.com/users/rl9322179/projects/1025?xx2l0=8FLdv
https://github.com/users/rl9322179/projects/1025?wbe0d=38
https://github.com/users/rl9322179/projects/1025?xudkt=coxx
https://github.com/users/rl9322179/projects/1025?udeco=25697
https://github.com/users/rl9322179/projects/1025?7E56k/04mMt=24
https://github.com/users/rl9322179/projects/1025?E362t/73400=30
https://github.com/users/rl9322179/projects/1025?mmele/kcwv=94
https://github.com/users/rl9322179/projects/1025?wvolw/69182=57
https://github.com/users/rl9322179/projects/1025/
https://github.com/users/rl9322179/projects/1025?/#/6wNC9=7uDXv
https://github.com/users/rl9322179/projects/1025/#/7xtNu=98
https://github.com/users/rl9322179/projects/1025?mbctl/#/txcd=64
https://github.com/users/sptmh16557105/projects/1037
https://github.com/users/sptmh16557105/projects/1037%3Ffullscreen%3Dtrue
https://www.github.com/users/sptmh16557105/projects/1037
http://github.com/users/sptmh16557105/projects/1037
https://github.com/users/sptmh16557105/projects/1037?fullscreen=true
https://github.com/users/sptmh16557105/projects/1037?ovI7e=IW6Zp
https://github.com/users/sptmh16557105/projects/1037?q6vyp=30
https://github.com/users/sptmh16557105/projects/1037?ppfef=wpvp
https://github.com/users/sptmh16557105/projects/1037?oiezi=99020
https://github.com/users/sptmh16557105/projects/1037?6EyhV/Mp59I=20
https://github.com/users/sptmh16557105/projects/1037?4V1OI/34814=60
https://github.com/users/sptmh16557105/projects/1037?imnyw/wpwy=38
https://github.com/users/sptmh16557105/projects/1037?qepyz/46992=28
https://github.com/users/sptmh16557105/projects/1037/
https://github.com/users/sptmh16557105/projects/1037?/#/p75f2=N3P4X
https://github.com/users/sptmh16557105/projects/1037/#/Zy8Ze=44
https://github.com/users/sptmh16557105/projects/1037?opoyq/#/hmqe=48
https://github.com/users/bvaoih007/projects/1040
https://github.com/users/bvaoih007/projects/1040%3Ffullscreen%3Dtrue
https://www.github.com/users/bvaoih007/projects/1040
http://github.com/users/bvaoih007/projects/1040
https://github.com/users/bvaoih007/projects/1040?fullscreen=true
https://github.com/users/bvaoih007/projects/1040?N6V01=voTcF
https://github.com/users/bvaoih007/projects/1040?NdM7c=86
https://github.com/users/bvaoih007/projects/1040?nknxl=belv
https://github.com/users/bvaoih007/projects/1040?vtoff=02709
https://github.com/users/bvaoih007/projects/1040?0Ux5L/x79OW=81
https://github.com/users/bvaoih007/projects/1040?2Lm74/50591=02
https://github.com/users/bvaoih007/projects/1040?eweud/dkox=91
https://github.com/users/bvaoih007/projects/1040?vecon/00465=08
https://github.com/users/bvaoih007/projects/1040/
https://github.com/users/bvaoih007/projects/1040?/#/Uvb41=oMvtU
https://github.com/users/bvaoih007/projects/1040/#/nWBb9=87
https://github.com/users/bvaoih007/projects/1040?wvouv/#/xxvu=53
https://github.com/users/sptmh16557105/projects/1038
https://github.com/users/sptmh16557105/projects/1038%3Ffullscreen%3Dtrue
https://www.github.com/users/sptmh16557105/projects/1038
http://github.com/users/sptmh16557105/projects/1038
https://github.com/users/sptmh16557105/projects/1038?fullscreen=true
https://github.com/users/sptmh16557105/projects/1038?50LfL=no502
https://github.com/users/sptmh16557105/projects/1038?l84YY=37
https://github.com/users/sptmh16557105/projects/1038?cpnuf=cgye
https://github.com/users/sptmh16557105/projects/1038?dffpe=51766
https://github.com/users/sptmh16557105/projects/1038?F0du2/M4M6M=31
https://github.com/users/sptmh16557105/projects/1038?gvvP6/73218=82
https://github.com/users/sptmh16557105/projects/1038?xevly/mldd=29
https://github.com/users/sptmh16557105/projects/1038?pyely/26234=24
https://github.com/users/sptmh16557105/projects/1038/
https://github.com/users/sptmh16557105/projects/1038?/#/57vgu=Xul5o
https://github.com/users/sptmh16557105/projects/1038/#/01MF8=43
https://github.com/users/sptmh16557105/projects/1038?vlxcm/#/nowy=65
https://github.com/users/rl9322179/projects/1026
https://github.com/users/rl9322179/projects/1026%3Ffullscreen%3Dtrue
https://www.github.com/users/rl9322179/projects/1026
http://github.com/users/rl9322179/projects/1026
https://github.com/users/rl9322179/projects/1026?fullscreen=true
https://github.com/users/rl9322179/projects/1026?r8aeL=L9bLJ
https://github.com/users/rl9322179/projects/1026?b95aa=59
https://github.com/users/rl9322179/projects/1026?btbir=takl
https://github.com/users/rl9322179/projects/1026?rckrt=96823
https://github.com/users/rl9322179/projects/1026?3ci11/3Kv48=11
https://github.com/users/rl9322179/projects/1026?AA6rt/81424=78
https://github.com/users/rl9322179/projects/1026?rdecb/busm=85
https://github.com/users/rl9322179/projects/1026?vajjr/75378=96
https://github.com/users/rl9322179/projects/1026/
https://github.com/users/rl9322179/projects/1026?/#/JmjKK=CmE9d
https://github.com/users/rl9322179/projects/1026/#/ECtc6=32
https://github.com/users/rl9322179/projects/1026?ijlse/#/reuk=90
https://github.com/users/bvaoih007/projects/1041
https://github.com/users/bvaoih007/projects/1041%3Ffullscreen%3Dtrue
https://www.github.com/users/bvaoih007/projects/1041
http://github.com/users/bvaoih007/projects/1041
https://github.com/users/bvaoih007/projects/1041?fullscreen=true
https://github.com/users/bvaoih007/projects/1041?Ey7H6=GY34n
https://github.com/users/bvaoih007/projects/1041?0FfV3=64
https://github.com/users/bvaoih007/projects/1041?iqxyy=qzpn
https://github.com/users/bvaoih007/projects/1041?fiepr=99256
https://github.com/users/bvaoih007/projects/1041?I30Gx/Y3EQn=59
https://github.com/users/bvaoih007/projects/1041?0gypP/61533=44
https://github.com/users/bvaoih007/projects/1041?rigpg/erhv=01
https://github.com/users/bvaoih007/projects/1041?noezg/45837=23
https://github.com/users/bvaoih007/projects/1041/
https://github.com/users/bvaoih007/projects/1041?/#/Nry94=23iPf
https://github.com/users/bvaoih007/projects/1041/#/jRMl9=27
https://github.com/users/bvaoih007/projects/1041?rxifg/#/vizf=16
https://github.com/users/sptmh16557105/projects/1039
https://github.com/users/sptmh16557105/projects/1039%3Ffullscreen%3Dtrue
https://www.github.com/users/sptmh16557105/projects/1039
http://github.com/users/sptmh16557105/projects/1039
https://github.com/users/sptmh16557105/projects/1039?fullscreen=true
https://github.com/users/sptmh16557105/projects/1039?O9wHX=o6i9w
https://github.com/users/sptmh16557105/projects/1039?RN4y6=01
https://github.com/users/sptmh16557105/projects/1039?eowhp=fryq
https://github.com/users/sptmh16557105/projects/1039?rfnwf=14357
https://github.com/users/sptmh16557105/projects/1039?pNYoG/4FNE7=45
https://github.com/users/sptmh16557105/projects/1039?WhG9f/36097=27
https://github.com/users/sptmh16557105/projects/1039?zxrig/fqzi=74
https://github.com/users/sptmh16557105/projects/1039?oaiza/94963=52
https://github.com/users/sptmh16557105/projects/1039/
https://github.com/users/sptmh16557105/projects/1039?/#/qXFIN=GP9aH
https://github.com/users/sptmh16557105/projects/1039/#/FQ4aG=16
https://github.com/users/sptmh16557105/projects/1039?iwgon/#/gwza=11
https://github.com/users/rl9322179/projects/1027
https://github.com/users/rl9322179/projects/1027%3Ffullscreen%3Dtrue
https://www.github.com/users/rl9322179/projects/1027
http://github.com/users/rl9322179/projects/1027
https://github.com/users/rl9322179/projects/1027?fullscreen=true
https://github.com/users/rl9322179/projects/1027?Q043F=3VFrz
https://github.com/users/rl9322179/projects/1027?Hev13=54
https://github.com/users/rl9322179/projects/1027?xiznh=ighh
https://github.com/users/rl9322179/projects/1027?gqwiv=41475
https://github.com/users/rl9322179/projects/1027?NZv7y/4YnNo=41
https://github.com/users/rl9322179/projects/1027?11xR5/67791=59
https://github.com/users/rl9322179/projects/1027?ozfny/hgri=63
https://github.com/users/rl9322179/projects/1027?xxovp/60472=77
https://github.com/users/rl9322179/projects/1027/
https://github.com/users/rl9322179/projects/1027?/#/X0g5g=i582P
https://github.com/users/rl9322179/projects/1027/#/Y2yFx=28
https://github.com/users/rl9322179/projects/1027?geppv/#/pxvx=72
https://github.com/users/bvaoih007/projects/1042
https://github.com/users/bvaoih007/projects/1042%3Ffullscreen%3Dtrue
https://www.github.com/users/bvaoih007/projects/1042
http://github.com/users/bvaoih007/projects/1042
https://github.com/users/bvaoih007/projects/1042?fullscreen=true
https://github.com/users/bvaoih007/projects/1042?3k8yB=Jp20j
https://github.com/users/bvaoih007/projects/1042?5CCT2=82
https://github.com/users/bvaoih007/projects/1042?sazjs=jgtt
https://github.com/users/bvaoih007/projects/1042?rjhpr=02662
https://github.com/users/bvaoih007/projects/1042?1ABR6/IpY2Q=17
https://github.com/users/bvaoih007/projects/1042?4zgrI/81279=92
https://github.com/users/bvaoih007/projects/1042?ckaas/bcra=38
https://github.com/users/bvaoih007/projects/1042?arkiz/84443=04
https://github.com/users/bvaoih007/projects/1042/
https://github.com/users/bvaoih007/projects/1042?/#/9tGKK=ICRg2
https://github.com/users/bvaoih007/projects/1042/#/at06S=19
https://github.com/users/bvaoih007/projects/1042?jjahc/#/bjzj=90
https://github.com/users/rl9322179/projects/1028
https://github.com/users/rl9322179/projects/1028%3Ffullscreen%3Dtrue
https://www.github.com/users/rl9322179/projects/1028
http://github.com/users/rl9322179/projects/1028
https://github.com/users/rl9322179/projects/1028?fullscreen=true
https://github.com/users/rl9322179/projects/1028?379PU=emD3m
https://github.com/users/rl9322179/projects/1028?H6LW1=77
https://github.com/users/rl9322179/projects/1028?ymfux=depf
https://github.com/users/rl9322179/projects/1028?dxplw=37509
https://github.com/users/rl9322179/projects/1028?fP62E/xG950=13
https://github.com/users/rl9322179/projects/1028?L1Mpg/54267=68
https://github.com/users/rl9322179/projects/1028?ynnuh/hhgn=79
https://github.com/users/rl9322179/projects/1028?eepwn/38937=78
https://github.com/users/rl9322179/projects/1028/
https://github.com/users/rl9322179/projects/1028?/#/v3uy1=6OXvE
https://github.com/users/rl9322179/projects/1028/#/19L4v=53
https://github.com/users/rl9322179/projects/1028?heonp/#/muwv=37
https://github.com/users/sptmh16557105/projects/1040
https://github.com/users/sptmh16557105/projects/1040%3Ffullscreen%3Dtrue
https://www.github.com/users/sptmh16557105/projects/1040
http://github.com/users/sptmh16557105/projects/1040
https://github.com/users/sptmh16557105/projects/1040?fullscreen=true
https://github.com/users/sptmh16557105/projects/1040?4BBZg=eFT7D
https://github.com/users/sptmh16557105/projects/1040?HAAq6=34
https://github.com/users/sptmh16557105/projects/1040?hcgqj=ukek
https://github.com/users/sptmh16557105/projects/1040?iaaaz=55741
https://github.com/users/sptmh16557105/projects/1040?u7MTV/KfoDx=46
https://github.com/users/sptmh16557105/projects/1040?OgTLe/48713=26
https://github.com/users/sptmh16557105/projects/1040?vnowl/wnnc=46
https://github.com/users/sptmh16557105/projects/1040?kvvmt/55803=16
https://github.com/users/sptmh16557105/projects/1040/
https://github.com/users/sptmh16557105/projects/1040?/#/eFuoG=d8d62
https://github.com/users/sptmh16557105/projects/1040/#/7nF2D=94
https://github.com/users/sptmh16557105/projects/1040?wwuol/#/gulw=15
https://github.com/users/bvaoih007/projects/1043
https://github.com/users/bvaoih007/projects/1043%3Ffullscreen%3Dtrue
