当前位置: 动力学知识库 > 问答 > 编程问答 >

How to restart program automatically if it crashes in Windows?

问题描述:

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?

网友答案:

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

网友答案:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: