问题描述
把项目打包成war包,放到tomcat中,但是经常过一段时间, tomcat就自动关闭了。
原因是这台服务器还运行其他项目,内存有时候不够用,导致windows杀死了较大的进程。
解决方法
写一个bat脚本,定时访问网址,如果访问失败则重启tomcat
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
rem 链接
set URL="http://localhost/**"
rem tomcat目录
set TOMCAT_HOME="C:\Users\Administrator\Desktop\apache-tomcat8"
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%\HuanCun
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,10) 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