windows下bat批处理实现守护进程(有日志)

windows下bat批处理实现守护进程(有日志)

分类: windows 515人阅读 评论(1) 收藏 举报
windows shell c

开发部的一个核心程序总是会自己宕机,然后需要手工去起,而这个服务的安全级别又很高,只有我可以操作,搞得我晚上老没法睡,昨晚实在受不了了,想起以前在hp-ux下写的shell守护进程,这回搞个windows下的bat版守护程序吧,当时晚上思路已经很迟钝了,就叫了个兄弟让他写了,上去后运行效果不错,至少昨晚我安心睡了7小时。
早上来把程序改完善一些,增加了记录等功能。
实现:

检查是否有notepad,要用的话就算成自己的进程名,如果进程宕了就过会自动重启(会在当前目录下生成一个start.bat)

@echo off

set _task=notepad.exe
set _svr=c:/windows/notepad.exe
set _des=start.bat

:checkstart
for /f "tokens=5" %%nin('qprocess.exe ^| find"%_task%"')do(
if %%n==%_task%(goto checkag)else goto startsvr
)



:startsvr
echo %time%
echo ********程序开始启动********
echo 程序重新启动于 %time%,请检查系统日志>> restart_service.txt
echo start %_svr%>%_des%
echo exit >>%_des%
start %_des%
set/p=.<nul
for /L %%i in(1 1 10)do set /p a=.<nul&ping.exe/n 2 127.0.0.1>nul
echo .
echo Wscript.Sleep WScript.Arguments(0)>%tmp%/delay.vbs
cscript //b//nologo%tmp%/delay.vbs 10000
del %_des% /Q
echo ********程序启动完成********
goto checkstart


:checkag
echo %time% 程序运行正常,10秒后继续检查..
echo Wscript.Sleep WScript.Arguments(0)>%tmp%/delay.vbs
cscript //b//nologo%tmp%/delay.vbs 10000

goto checkstart



以上高手的批处理需要qprocess.exe 我改了一下,结果如下:


@echo off

set _task=javatikaserver.exe
set _svr=javatikaserver.exe -jar server-1.4.jar
set _des=start.bat

:checkstart
rem for /f "tokens=5" %%n in ('tasklist.exe ^| find "%_task%" ') do (
rem if %%n==%_task% (goto checkag) else goto startsvr
rem )
tasklist | find /i "%_task%"&&(goto checkag)|| (goto startsvr) &&exit


:startsvr
echo %time%
echo ********程序开始启动********
echo 程序重新启动于 %time% ,请检查系统日志 >> restart_service.txt
rem echo start %_svr% > %_des%
echo %_svr% > %_des%
rem echo exit >> %_des%
start %_des%
set/p=.<nul
for /L %%i in (1 1 3) do set /p a=.<nul&ping.exe /n 2 127.0.0.1>nul
echo .
rem echo Wscript.Sleep WScript.Arguments(0) >%tmp%/delay.vbs
rem cscript //b //nologo %tmp%/delay.vbs 10000
rem del %_des% /Q
echo ********程序启动完成********
goto checkstart


:checkag
echo %time% 程序运行正常,10秒后继续检查..
echo Wscript.Sleep WScript.Arguments(0) >%tmp%/delay.vbs
cscript //b //nologo %tmp%/delay.vbs 10000
goto checkstart

你可能感兴趣的:(windows)