@echo off
setlocal enabledelayedexpansion
rem 链接
set URL="localhost:8080/XXX.jsp"
rem tomcat目录
set TOMCAT_HOME=D:\apache-tomcat-7.0.100-windows-x64\apache-tomcat-7.0.100
rem 关闭tomcat命令的路径
set CLOSE_CMD=%TOMCAT_HOME%\bin\shutdown.bat
rem 启动tomcat命令的路径
set START_CMD=%TOMCAT_HOME%\bin\startup.bat
rem tomcat缓存目录
set TOMCAT_CACHE=%TOMCAT_HOME%\work
rem 日志文件的路径
set LOG_PATH=%TOMCAT_HOME%\check.log
rem 每次检测完后等待时间,再进行下一次检测,秒,若将程序部署到系统计划任务,可忽略
set TIME_WAIT=5
:loop
set httpcode=0
cd /d %TOMCAT_HOME%
echo %date% %time%
echo 'begin checking tomcat'
echo %date% %time% >>%LOG_PATH%
for /l %%i in (1,1,3) do (
echo %%i
for /f "delims=" %%r in ('curl -sL -w "%%{http_code}" %URL% -o /dev/null') do (
set httpcode=%%r
if !httpcode!==200 (
GOTO :OUTFOR
)
)
)
:OUTFOR
echo %httpcode% >>%LOG_PATH%
if not %httpcode%==200 (
echo close tomcat >>%LOG_PATH%
call %CLOSE_CMD%
timeout -t 5 >nul
echo success to close tomcat >>%LOG_PATH%
rd /S /Q %TOMCAT_CACHE%
echo start tomcat >>%LOG_PATH%
call %START_CMD%
echo success to start tomcat
echo success to start tomcat >>%LOG_PATH%
) else (
echo the tomcat run is ok
echo the tomcat run is ok >>%LOG_PATH%
)
timeout -t 3 >nul
rem 若将脚本程序部署到系统计划程序中,将以下代码可注释
timeout -t %TIME_WAIT% >nul
goto loop