How to restart program automatically if it crashes in Windows?

The usual approach is to run what is known as a guardian process. This is a separate process, often a service, that monitors the state of the main process. When the guardian detects that the main service has died, it re-spawns it.

To the very best of my knowledge, there is not built in Windows functionality to do this for you.


Notice: running self-looping bat files can be useful, but unless you know what you're doing, they can wreak all kinds of havoc. This goes especially if you run them on startup. You have been warned.

Anyway. I just remembered something from my 286 days, when I played around a lot with BAT files. If you write the file

yourprogram.exe
some other event

the BAT file will run yourprogram, and then pause and wait around in the background until the program exits. After that it will run "some other event". This used to be kind of annoying if you wanted to run multiple things at once, but here it's actually useful. Using this, it's possible to make it run a loop that restarts the program (and reruns the bat file) as soon as it exits. Combine this with https://superuser.com/questions/62525/run-a-completly-hidden-batch-file, and you'll never even see it happening.

The final BAT file ("restart.bat" in this example) will look something like:

c:\[location]\yourprogram.exe
wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"

That's about it. Start the program (on startup via task or even just startup folder) with line 2, and this ought to solve your problem :)

Oh, if you want to stop the loop, just rename the bat file or put "// " in front of the two lines, save it, and exit the program.

If the program you are running requires admin rights, the solution I found was using psexec (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) to run both the program and the bat with elevated privileges. In that case the BAT will look like:

c:\[location]\psexec -h c:\[location]\yourprogram.exe
c:\[location]\psexec -h wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"

Then you run the bat as administrator, or run the second line (without the psexec part) from task scheduler with elevated privileges. BEWARE: running it as a normal user and clicking "no" on the UAC prompt gave me a BSOD, probably because it looped "can't run program because of lacking privileges" a couple of billion times or something :)


There are several ways to create a process supervisor/guardian process on Windows. First, is to leverage windows command line capabilities. Create a bat file:

@echo off
:start
start /w "your app to watch.exe"
goto start

start /w will wait for the process to exit. When the process crashes and exits, the bat script will relaunch it.

Another option is to use free supervisor tool https://github.com/chebum/Supervisor. It allows to restart the crashed app, plus it allows to monitor two or more apps at once and it will automatically close these apps when supervisor's window is closed.