定時檢查服務批處理,發現服務中止當即啓動服務nginx
@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 )
定時檢查進程批處理。發現進程不存在當即啓動進程。bash
@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 )
以上存爲批處理文件。設置計劃任務定時執行便可。code