@echo off

echo "check %1." >> log.file

set srvname=%1
for /f "skip=3 tokens=4" %%i in ('sc query %srvname%') do set "zt=%%i" &goto :next
:next
if /i "%zt%"=="RUNNING" (
:服务运行正常,返回1.
echo 1
) else (
:服务运行异常,返回0
echo 0
sc start %srvname%
echo "%srvname% start." >> log.file
)


定时检查进程批处理。发现进程不存在立即启动进程。

@echo off

echo "check %1." >> log.file

set pidname=%1
set batname=%2
for /f %%i in ('tasklist /FI "IMAGENAME eq %pidname%"^| find /C "%pidname%"') do set "zt=%%i" &goto :next

:next
echo %zt%
if /i %zt% gtr 0 (
:进程运行正常,返回1.
echo 1
) else (
:进程运行异常,返回0。立刻启动进程
echo 0
call d:\nginx-1.4.7\%batname%
echo "%pidname% start." >> log.file
)


以上存为批处理文件。设置计划任务定时执行即可。