How to Start and Stop Apache Tomcat from a Batch Script
Apache Tomcat is the most widely used Java servlet container, powering countless Java web applications and REST APIs. Managing Tomcat's lifecycle (start, stop, restart) from Batch Script is essential for deployment automation, maintenance windows, and service recovery. While Tomcat ships with its own startup.bat and shutdown.bat scripts, wrapping them in custom Batch scripts adds health checking, logging, and error handling.
In this guide, we will explore how to start, stop, and manage Apache Tomcat from a Batch Script, covering environment setup, graceful shutdowns, and production-grade management workflows.
Prerequisites
Tomcat requires a Java Runtime Environment (JRE) or JDK. Ensure these environment variables are set:
@echo off
setlocal
:: Required environment variables
set "CATALINA_HOME=C:\Tomcat"
set "JAVA_HOME=C:\Program Files\Java\jdk-17"
:: Verify Tomcat installation
if not exist "%CATALINA_HOME%\bin\catalina.bat" (
echo [ERROR] CATALINA_HOME not valid: %CATALINA_HOME%
pause
exit /b 1
)
:: Verify Java installation
if not exist "%JAVA_HOME%\bin\java.exe" (
echo [ERROR] JAVA_HOME not valid: %JAVA_HOME%
pause
exit /b 1
)
echo [OK] Tomcat: %CATALINA_HOME%
echo [OK] Java: %JAVA_HOME%
Method 1: Basic Start and Stop
@echo off
setlocal
set "CATALINA_HOME=C:\Tomcat"
if not exist "%CATALINA_HOME%\bin\startup.bat" (
echo [ERROR] Tomcat not found at: %CATALINA_HOME%
pause
exit /b 1
)
if "%~1"=="" (
echo Usage: tomcat.bat [start^|stop^|restart]
pause
exit /b 1
)
if /i "%~1"=="start" (
echo Starting Tomcat...
call "%CATALINA_HOME%\bin\startup.bat"
echo [OK] Tomcat start command issued.
) else if /i "%~1"=="stop" (
echo Stopping Tomcat...
call "%CATALINA_HOME%\bin\shutdown.bat"
echo [OK] Tomcat shutdown command issued.
) else if /i "%~1"=="restart" (
echo Restarting Tomcat...
call "%CATALINA_HOME%\bin\shutdown.bat"
timeout /t 10 /nobreak >nul
call "%CATALINA_HOME%\bin\startup.bat"
echo [OK] Tomcat restarted.
) else (
echo [ERROR] Unknown command: %~1
echo Usage: tomcat.bat [start^|stop^|restart]
exit /b 1
)
pause
Method 2: Start with Health Check
Start Tomcat and verify it is responding:
@echo off
setlocal enabledelayedexpansion
set "CATALINA_HOME=C:\Tomcat"
set "health_url=http://localhost:8080"
set "max_retries=12"
set "retry_delay=5"
set "http_timeout=3"
if not exist "%CATALINA_HOME%\bin\startup.bat" (
echo [ERROR] Tomcat not found at: %CATALINA_HOME%
exit /b 1
)
echo =============================================
echo STARTING APACHE TOMCAT
echo =============================================
echo.
:: Check if already running
powershell -NoProfile -Command ^
"try { Invoke-WebRequest -Uri '%health_url%' -UseBasicParsing -TimeoutSec 2 | Out-Null; exit 0 }" ^
"catch { exit 1 }" >nul 2>&1
if %errorlevel%==0 (
echo [INFO] Tomcat is already running at %health_url%.
pause
exit /b 0
)
:: Start Tomcat
echo Starting Tomcat...
call "%CATALINA_HOME%\bin\startup.bat" >nul 2>&1
:: Wait for startup
echo Waiting for Tomcat to become ready...
set "attempt=0"
:check
set /a attempt+=1
echo [!attempt!/%max_retries%] Checking...
powershell -NoProfile -Command ^
"try {" ^
" $r = Invoke-WebRequest -Uri '%health_url%' -UseBasicParsing -TimeoutSec %http_timeout%;" ^
" if ($r.StatusCode -eq 200) { exit 0 } else { exit 1 }" ^
"} catch { exit 1 }" >nul 2>&1
if !errorlevel!==0 (
echo.
echo [SUCCESS] Tomcat is running at %health_url%
exit /b 0
)
if !attempt! geq %max_retries% (
echo.
echo [ERROR] Tomcat did not start within !attempt! attempts.
echo Check logs: %CATALINA_HOME%\logs\
exit /b 1
)
timeout /t %retry_delay% /nobreak >nul
goto check
Method 3: Graceful Stop with Process Verification
@echo off
setlocal enabledelayedexpansion
set "CATALINA_HOME=C:\Tomcat"
set "shutdown_timeout=30"
set "port=8080"
if not exist "%CATALINA_HOME%\bin\shutdown.bat" (
echo [ERROR] Tomcat not found at: %CATALINA_HOME%
exit /b 1
)
echo Stopping Tomcat gracefully...
:: Send shutdown command
call "%CATALINA_HOME%\bin\shutdown.bat" >nul 2>&1
:: Wait for the port to become free (indicates Tomcat has fully stopped)
echo Waiting for Tomcat to stop (timeout: %shutdown_timeout%s^)...
set "elapsed=0"
:wait_stop
timeout /t 2 /nobreak >nul
set /a elapsed+=2
:: Check if anything is still listening on the Tomcat port
netstat -an | findstr ":%port% " | findstr "LISTENING" >nul 2>&1
if !errorlevel!==0 (
if !elapsed! geq %shutdown_timeout% (
echo [WARNING] Tomcat did not stop gracefully within %shutdown_timeout%s.
echo Attempting force stop...
:: Find the PID listening on the port and kill it
for /f "tokens=5" %%P in ('netstat -ano ^| findstr ":%port% " ^| findstr "LISTENING"') do (
echo Killing PID %%P...
taskkill /f /pid %%P >nul 2>&1
)
goto stopped
)
echo Still running... (!elapsed!s / %shutdown_timeout%s^)
goto wait_stop
)
:stopped
echo.
echo [OK] Tomcat stopped.
Method 4: Tomcat Service Manager
A comprehensive management script:
@echo off
title Tomcat Manager
setlocal enabledelayedexpansion
set "CATALINA_HOME=C:\Tomcat"
set "JAVA_HOME=C:\Program Files\Java\jdk-17"
set "port=8080"
set "http_timeout=2"
if not exist "%CATALINA_HOME%\bin\catalina.bat" (
echo [ERROR] Tomcat not found at: %CATALINA_HOME%
pause
exit /b 1
)
:menu
cls
echo =============================================
echo APACHE TOMCAT MANAGER
echo Home: %CATALINA_HOME%
echo =============================================
echo.
:: Check status
set "status_text=STOPPED"
powershell -NoProfile -Command ^
"try { Invoke-WebRequest -Uri 'http://localhost:%port%' -UseBasicParsing -TimeoutSec %http_timeout% | Out-Null; exit 0 }" ^
"catch { exit 1 }" >nul 2>&1
if !errorlevel!==0 (
set "status_text=RUNNING (port %port%^)"
)
echo Status: !status_text!
echo.
echo [1] Start
echo [2] Stop
echo [3] Restart
echo [4] View logs (last 30 lines^)
echo [5] Deploy WAR file
echo [6] List deployed apps
echo [0] Exit
echo.
set /p "choice=Select: "
if "!choice!"=="1" (
echo.
echo Starting Tomcat...
call "%CATALINA_HOME%\bin\startup.bat"
timeout /t 5 /nobreak >nul
goto menu
)
if "!choice!"=="2" (
echo.
echo Stopping Tomcat...
call "%CATALINA_HOME%\bin\shutdown.bat"
timeout /t 5 /nobreak >nul
goto menu
)
if "!choice!"=="3" (
echo.
echo Restarting Tomcat...
call "%CATALINA_HOME%\bin\shutdown.bat"
timeout /t 10 /nobreak >nul
call "%CATALINA_HOME%\bin\startup.bat"
timeout /t 5 /nobreak >nul
goto menu
)
if "!choice!"=="4" (
echo.
echo Last 30 lines of catalina log:
echo ================================
set "log_found=0"
for /f "delims=" %%L in ('dir /b /o-d "%CATALINA_HOME%\logs\catalina*.log" 2^>nul') do (
if !log_found!==0 (
set "log_found=1"
powershell -NoProfile -Command ^
"Get-Content '%CATALINA_HOME%\logs\%%L' -Tail 30"
)
)
if !log_found!==0 (
echo No catalina log files found.
)
pause
goto menu
)
if "!choice!"=="5" (
echo.
set /p "war_file=Path to WAR file: "
if not defined war_file (
echo [ERROR] No file specified.
) else if not exist "!war_file!" (
echo [ERROR] File not found: !war_file!
) else (
echo !war_file! | findstr /i "\.war$" >nul
if !errorlevel! neq 0 (
echo [ERROR] File is not a .war file.
) else (
copy "!war_file!" "%CATALINA_HOME%\webapps\" >nul
if !errorlevel!==0 (
echo [OK] WAR deployed. Tomcat will auto-deploy it.
) else (
echo [ERROR] Copy failed.
)
)
)
pause
goto menu
)
if "!choice!"=="6" (
echo.
echo Deployed applications:
echo ----------------------
set "app_found=0"
for /f "delims=" %%D in ('dir /b /ad "%CATALINA_HOME%\webapps\" 2^>nul') do (
echo [DIR] %%D
set "app_found=1"
)
for %%W in ("%CATALINA_HOME%\webapps\*.war") do (
echo [WAR] %%~nxW
set "app_found=1"
)
if !app_found!==0 (
echo No applications deployed.
)
pause
goto menu
)
if "!choice!"=="0" exit /b 0
echo [ERROR] Invalid selection.
timeout /t 2 /nobreak >nul
goto menu
Method 5: Running Tomcat as a Windows Service
For production servers, Tomcat should run as a Windows service:
@echo off
setlocal
set "CATALINA_HOME=C:\Tomcat"
set "service_name=Tomcat9"
if not exist "%CATALINA_HOME%\bin\service.bat" (
echo [ERROR] Tomcat service script not found at: %CATALINA_HOME%\bin\service.bat
pause
exit /b 1
)
:: Verify Admin (required for service management)
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required for service management.
pause
exit /b 1
)
if "%~1"=="" (
echo =============================================
echo TOMCAT SERVICE MANAGEMENT
echo =============================================
echo.
echo Current status:
sc query %service_name% 2>nul | findstr "STATE"
if %errorlevel% neq 0 (
echo Service "%service_name%" is not installed.
)
echo.
echo Usage: tomcat_service.bat [install^|start^|stop^|restart^|status^|uninstall]
pause
exit /b 0
)
if /i "%~1"=="install" (
echo Installing Tomcat as service "%service_name%"...
call "%CATALINA_HOME%\bin\service.bat" install %service_name%
) else if /i "%~1"=="uninstall" (
echo Removing service "%service_name%"...
net stop %service_name% >nul 2>&1
call "%CATALINA_HOME%\bin\service.bat" remove %service_name%
) else if /i "%~1"=="start" (
echo Starting service...
net start %service_name%
) else if /i "%~1"=="stop" (
echo Stopping service...
net stop %service_name%
) else if /i "%~1"=="restart" (
echo Restarting service...
net stop %service_name% 2>nul
timeout /t 5 /nobreak >nul
net start %service_name%
) else if /i "%~1"=="status" (
sc query %service_name%
) else (
echo [ERROR] Unknown command: %~1
echo Usage: tomcat_service.bat [install^|start^|stop^|restart^|status^|uninstall]
exit /b 1
)
pause
Common Mistakes
The Wrong Way: Starting Without JAVA_HOME
:: WRONG - Tomcat cannot find Java
call C:\Tomcat\bin\startup.bat
:: Error: JAVA_HOME is not defined
Output Concern:
Tomcat requires JAVA_HOME or JRE_HOME to be set. Without it, catalina.bat reports an error and exits. Always set JAVA_HOME before calling Tomcat scripts.
The Wrong Way: Force-Killing Without Graceful Shutdown
:: WRONG - Kills Java process immediately
taskkill /f /im java.exe
:: Active requests are terminated, sessions are lost, data may be corrupted
Always attempt a graceful shutdown with shutdown.bat first. Force-kill only after a timeout if the graceful shutdown fails.
Best Practices
- Set
JAVA_HOMEexplicitly: Do not rely on system PATH for Java detection. - Use
callfor Tomcat scripts:startup.batandshutdown.batare Batch files requiringcall. - Verify startup with health checks: Do not assume Tomcat is ready immediately after
startup.bat. - Use graceful shutdown first: Allow active requests to complete before stopping.
- Run as a service in production: Windows services auto-start on boot and run without a logged-in user.
Conclusion
Starting and stopping Apache Tomcat from a Batch Script involves calling Tomcat's built-in startup.bat and shutdown.bat scripts with proper JAVA_HOME configuration. By adding health checks after startup, implementing graceful shutdown with timeout-based fallback to force-kill, deploying WAR files through the webapps directory, and running Tomcat as a Windows service for production use, administrators build robust Tomcat lifecycle management that integrates seamlessly into deployment pipelines and maintenance workflows.