Batch script to restart program if it closes.

NST-LI
NST-LI used Ask the Experts™
on
We host a server that runs a program(rather than service) with a gui at the desktop that cannot be closed. Recently, we have been having issues where it has been closing which is a big problem. Is there a batch script that can be created to start the program if it is closed automatically?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
You could setup a Scheduled Task every x minutes to check if your program is not running then start it.

Set ProgramPath=C:\Windows
Set ProgramName=Regedit.exe
 
tasklist /FI "IMAGENAME eq %ProgramName%" | find /i "%ProgramName%"
 
if errorlevel 1 Start "Starting %ProgramName%" "%ProgramPath%\%ProgramName%"

Open in new window

Author

Commented:
TY for your response AmazingTech,

I have created a batch script using your code and tested on my PC with a random program. I set a scheduled task to run every 10 minutes and the result was a new instance of the program(VLC Player was the test subject) was created. I actually don't understand the code so I can't tell what does what in each line. I have attached some screenshots of what I have done if your curious.

script.bmp
batchscript.bmp
Top Expert 2009
Commented:
I tried to make it easy to change. You should only need to modify the first 2 set statements.

So for your case your batchscript.bat should be:
Set ProgramPath=C:\Program Files\VideoLAN\VLC
Set ProgramName=vlc.exe
 
tasklist /FI "IMAGENAME eq %ProgramName%" | find /i "%ProgramName%"
 
if errorlevel 1 Start "Starting %ProgramName%" "%ProgramPath%\%ProgramName%"
 

Open in new window

Top Expert 2009
Commented:
Or if you didn't want to use variables. You could hard code the script to.

Tasklist without using the filter will produce a list of all programs running and put it through the find command. It is probably faster to filter the tasklist to only the program we're after.
tasklist | find /i "vlc.exe"
if errorlevel 1 start "This is just a notice starting vlc.exe" "c:\Program Files\VideoLAN\VLC\vlc.exe"

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial