Friday, May 24, 2024

Keep a program running at all times - if a program closes, have it restart automatically in Windows

First, create a batch file to monitor the program and restart it-

@echo off
setlocal enabledelayedexpansion

set EXE_PATH="C:\path\to\MyProgram.exe"
set EXE_NAME=MyProgram.exe

:start_program
start "" %EXE_PATH%
echo Program started. Monitoring...

:monitor
REM Check if the program is running
tasklist /FI "IMAGENAME eq %EXE_NAME%" 2>NUL | find /I "%EXE_NAME%" >NUL
if "%ERRORLEVEL%"=="0" (
    REM Program is running, wait and check again
    timeout /t 1 >nul
    goto monitor
)

REM Program is not running, restart it
echo Program closed. Restarting...
goto start_program



Next, create a VBS script to run the batch file-

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c C:\path\to\monitor.bat", 0, False  




Finally, set the vbs script to run at windows startup-

Press Win + R to open the Run dialog.
Type shell:startup and press Enter. This opens the Startup folder.
Create a shortcut to the VBS script in the Startup folder.


Adam Cushing
www.adamcushing.com

No comments:

Post a Comment