21

How can I start my program automatically if it crashes on windows 2003 server? Sometimes my program just crashes, is there a way in windows or settings that I can set?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Zahid
  • 1,822
  • 4
  • 18
  • 26
  • 1
    What kind of program? A windows service or a regular app? – Joachim Isaksson Feb 18 '12 at 19:07
  • 2
    Of course you want to make your program crash free. That's a great ideal to strive for. But even the most robust programs can crash occasionally. If you are in a server environment, it can make a lot of sense to restart when a crash occurs. You can still log the crashes and try to eliminate them, but your users will be a lot happier without extended downtime for every crash. – aggieNick02 Jan 16 '18 at 23:31
  • Windows services don't need restarting by the user. Windows will do that automatically. – Gogu CelMare Aug 22 '22 at 07:33

7 Answers7

21

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.

Ivan Nikitin
  • 3,578
  • 27
  • 39
17

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 3
    Built-in feature for services, Recovery tab. – Hans Passant Feb 18 '12 at 22:22
  • 3
    I rather imagined that the use of the word "program" implied that the question concerned a desktop program. – David Heffernan Feb 18 '12 at 22:41
  • 1
    Nah, a service is a true blooded exe. Just a couple of conventions to keep the user in control. – Hans Passant Feb 18 '12 at 22:48
  • 1
    Well, services run in a different session and so on. Naturally they are just executables like any other executable. If the "program" is a service then recovery settings are the way to go without doubt. And a "program" that should run all the time most likely should be a service in any case. – David Heffernan Feb 18 '12 at 22:52
  • Unfortunately its a program and not a service.so I need help with restarting the program. Is thr any 3rd party utilty to do this job? – Zahid Feb 23 '12 at 18:51
  • It's probably less than 100 lines of code. Busy loop with a Sleep delay. Find your process each time round. If you can't find it, then start it. – David Heffernan Feb 23 '12 at 18:54
8

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 :)

Community
  • 1
  • 1
Johan
  • 81
  • 1
  • 1
  • 2
    Nice concept. You could even avoid a batch file and invisible.vbs, and do it in one VBScript instead e.g.: 'Keep the calculator open Set objShell = WScript.CreateObject("WScript.Shell") ObjShell.run "calc.exe", 1, true ObjShell.run (Wscript.ScriptFullName) – Jimadine Aug 15 '15 at 12:24
5

You can use RegisterApplicationRestart.

"If you register for restart and the application encounters an unhandled exception or is not responsive, the user is offered the opportunity to restart the application; the application is not automatically restarted without the user's consent. "

For automatic restart without user intervention, there is also RestartOnCrash. Works with all Windows versions.

Alasdair King
  • 106
  • 1
  • 4
3

I was looking for something similar. There are two options to handle this - either you can write a small script by yourself or use something that is already existing.

After some googling I came across this nice list. The blogger has compiled about 8 tools to automatically restart a crashed or closed application.

Mandeep Janjua
  • 15,583
  • 4
  • 29
  • 24
1

You may use some special app like BDV SystemEvents or any other. It allows you to specify application which will be started if some another application is closed. Specify the same application as a Condition and as an Action and you will get expected results.

Denis
  • 11
  • 2
1

Unfortunately there are no settings in Windows to automatically restart a regular program when it crashes.

Do you need to actively interact with your application's GUI? Some of the Service Wrappers (designed to run any application as a Windows Service) will monitor your application and restart it when it fails, but be sure investigate Session 0 Isolation to ensure that it won't get in the way.

CoreTech
  • 2,345
  • 2
  • 17
  • 24