How to Automatically Restart a Crashed Application in Batch Script
Software crashes can cause downtime for critical services, displays, or data-collection tools. Instead of relying on a human to notice that a window has disappeared, you can use a Batch script as a "Supervisor." By wrapping your application inside a monitoring loop, the script can detect the moment the application process terminates and immediately relaunch it.
This guide will explain two ways to handle automatic restarts: the "Parent-Wait" method and the "Background-Polling" method.
Method 1: The Parent-Wait Method (Simplest)
If the Batch script is the one that starts the application, it can simply wait for the process to end. As soon as the application is closed (or crashes), the script moves to the next line, which loops back to the start.
The Auto-Restart Wrapper
@echo off
setlocal
set "AppPath=C:\Apps\MyService\MyService.exe"
set "RestartDelay=5"
set "LogFile=%~dp0restart_log.txt"
:: Verify the application exists
if not exist "%AppPath%" (
echo [ERROR] Application not found: %AppPath%
pause
exit /b 1
)
echo [SUPERVISOR] Starting Application Monitor...
echo [SUPERVISOR] Application: %AppPath%
echo [SUPERVISOR] The application will be restarted automatically if it closes.
echo.
echo [%date% %time%] Supervisor started for %AppPath% >> "%LogFile%"
:RestartLoop
echo [%date% %time%] Starting application...
:: Use /WAIT to make the script pause here until the app closes
start /wait "" "%AppPath%"
set "ExitCode=%errorlevel%"
echo [%date% %time%] Application exited with code: %ExitCode%
echo [%date% %time%] Application exited (code: %ExitCode%) >> "%LogFile%"
echo [%date% %time%] Relaunching in %RestartDelay% seconds...
timeout /t %RestartDelay% /nobreak >nul
goto :RestartLoop
Why this works:
start /wait: This is the key command. The Batch script pauses while the application is open. It only continues to thegotoline after the application process terminates.
Method 2: The Background-Polling Method
If the application is already running or if it "detaches" (launches and exits quickly while leaving a background process), you need to poll the system task list.
@echo off
setlocal
set "AppExe=mytool.exe"
set "AppPath=C:\Tools\mytool.exe"
set "LogFile=%~dp0restart_audit.log"
set "PollInterval=30"
:: Verify the application exists
if not exist "%AppPath%" (
echo [ERROR] Application not found: %AppPath%
pause
exit /b 1
)
title Supervisor: Monitoring %AppExe%
echo [SUPERVISOR] Monitoring %AppExe% every %PollInterval% seconds...
echo [%date% %time%] Supervisor started >> "%LogFile%"
:: Initial delay to allow the system to stabilize
timeout /t 10 /nobreak >nul
:CheckLoop
:: Check if the process is running
tasklist /fi "imagename eq %AppExe%" /nh 2>nul | findstr /i "%AppExe%" >nul
if %errorlevel% neq 0 (
echo [%date% %time%] ALERT: %AppExe% not found. Relaunching...
start "" "%AppPath%"
echo [%date% %time%] Restarted %AppExe% >> "%LogFile%"
:: Wait for the application to initialize before next check
timeout /t %PollInterval% /nobreak >nul
)
:: Wait before checking again
timeout /t %PollInterval% /nobreak >nul
goto :CheckLoop
Implementing a "Crash Count" Limit
A professional script should prevent a "Crash Loop." If your app crashes repeatedly, it's likely a configuration error that restarting won't fix. Adding a counter stops the cycle before it overwhelms the system.
@echo off
setlocal enabledelayedexpansion
set "AppPath=C:\Apps\my_app.exe"
set "MaxFails=3"
set "RestartDelay=10"
set "LogFile=%~dp0crash_log.txt"
set /a "fails=0"
:: Verify the application exists
if not exist "%AppPath%" (
echo [ERROR] Application not found: %AppPath%
pause
exit /b 1
)
echo [SUPERVISOR] Crash limit: %MaxFails% consecutive failures.
:supervisor
echo [%date% %time%] Starting application (failure count: !fails!)...
start /wait "" "%AppPath%"
set "ExitCode=!errorlevel!"
:: Exit code 0 usually means intentional close, so reset the counter
if !ExitCode! equ 0 (
echo [INFO] Application closed normally (exit code 0).
set /a "fails=0"
) else (
set /a "fails+=1"
echo [WARNING] Application crashed with exit code: !ExitCode! (failure !fails! / %MaxFails%)
echo [%date% %time%] Crash detected. Exit code: !ExitCode!. Count: !fails! >> "%LogFile%"
)
if !fails! geq %MaxFails% (
echo.
echo [CRITICAL] Application crashed %MaxFails% times consecutively.
echo Manual intervention required.
echo [%date% %time%] CRITICAL: %MaxFails% consecutive failures. Supervisor stopped. >> "%LogFile%"
pause
exit /b 1
)
echo [ACTION] Restarting in %RestartDelay% seconds... (Failure count: !fails!)
timeout /t %RestartDelay% /nobreak >nul
goto :supervisor
How to Avoid Common Errors
Wrong Way: Restarting too fast
If your app crashes because of a "Port in use" error, and your script restarts it immediately, the port might not have been released by Windows yet.
Correct Way: Always include a timeout /t 5 or timeout /t 10 before the restart. This gives the system time to release resources from the crashed process.
Problem: Multiple instances
If your script launches the app but doesn't wait (Method 1) or doesn't check properly (Method 2), you could end up with dozens of instances of your app running, eventually crashing the entire computer.
Best Practice: Use start /wait for local tools (Method 1) and tasklist checks for background services (Method 2).
Best Practices and Rules
1. Minimal UI
If the supervisor is for a server, run the Batch window minimized using start /min. This prevents the console window from cluttering the desktop while still performing its job.
2. Administrator Elevation
If your application needs admin rights to run, your supervisor script must also be run as an Administrator. An unelevated Batch script cannot reliably launch an elevated EXE.
3. Absolute Paths
Always use full, absolute paths for the application location. When a crash occurs, the "Working Directory" of the script might shift, causing relative paths to fail.
Conclusions
Building an automatic restarter in Batch script is a simple way to increase the resilience of your software environment. Whether you use the simple start /wait logic for foreground tasks or the tasklist polling method for background agents, you are adding a self-healing layer to your infrastructure. These scripts ensure that minor software glitches don't lead to hours of downtime.