如果程序在 Windows 中崩溃,如何自动重新启动程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9344235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:15:05  来源:igfitidea点击:

How to restart program automatically if it crashes in Windows?

windowswindows-server-2003

提问by Zahid

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?

如果程序在 Windows 2003 服务器上崩溃,我如何自动启动它?有时我的程序会崩溃,我可以在 Windows 或设置中进行设置吗?

回答by David Heffernan

The usual approach is to run what is known as a guardianprocess. 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.

据我所知,没有内置的 Windows 功能可以为您执行此操作。

回答by Johan

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.

注意:运行自循环 bat 文件可能很有用,但除非您知道自己在做什么,否则它们可能会造成各种破坏。如果您在启动时运行它们,则尤其如此。你被警告了。

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

反正。我只记得我 286 天的一些事情,当时我经常玩 BAT 文件。如果你写文件

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.

BAT 文件将运行您的程序,然后在后台暂停并等待直到程序退出。之后它将运行“其他一些事件”。如果您想一次运行多个东西,这曾经有点烦人,但在这里它实际上很有用。使用它,可以让它运行一个循环,在程序退出时立即重新启动程序(并重新运行 bat 文件)。将此与https://superuser.com/questions/62525/run-a-completly-hidden-batch-file结合使用,您甚至永远不会看到它发生。

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

最终的 BAT 文件(在本例中为“restart.bat”)将类似于:

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

就是这样。用第 2 行启动程序(通过任务启动,甚至只是启动文件夹),这应该可以解决您的问题:)

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.

哦,如果你想停止循环,只需将bat文件重命名或在两行前面加上“//”,保存,然后退出程序。

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:

如果您运行的程序需要管理员权限,我找到的解决方案是使用 psexec ( http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) 以提升的权限运行程序和 bat。在这种情况下,BAT 将如下所示:

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

然后您以管理员身份运行 bat,或使用提升的权限从任务调度程序运行第二行(没有 psexec 部分)。当心:以普通用户身份运行它并在 UAC 提示上单击“否”给了我一个蓝屏,可能是因为它循环了“由于缺乏权限而无法运行程序”数十亿次或其他原因:)

回答by Mandeep Janjua

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.

经过一番谷歌搜索后,我发现了这个不错的列表。博主整理了大约8个工具来自动重启崩溃或关闭的应用程序。

回答by Alasdair King

You can use RegisterApplicationRestart.

您可以使用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.

对于无需用户干预的自动重启,还有RestartOnCrash。适用于所有 Windows 版本。

回答by Ivan Nikitin

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:

有多种方法可以在 Windows 上创建进程主管/监护人进程。首先,是利用 windows 命令行功能。创建一个bat文件:

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

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

start /w将等待进程退出。当进程崩溃并退出时,bat 脚本将重新启动它。

Another option is to use free supervisortool 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.

另一种选择是使用免费supervisor工具https://github.com/chebum/Supervisor。它允许重新启动崩溃的应用程序,此外它还允许一次监视两个或多个应用程序,并且当主管的窗口关闭时它会自动关闭这些应用程序。

回答by CoreTech

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

不幸的是,Windows 中没有设置可以在常规程序崩溃时自动重新启动它。

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 Isolationto ensure that it won't get in the way.

您是否需要主动与应用程序的 GUI 交互?一些服务包装器(旨在将任何应用程序作为 Windows 服务运行)将监视您的应用程序并在它失败时重新启动它,但一定要调查会话 0 隔离以确保它不会妨碍您。

回答by Denis

You may use some special app like BDV SystemEventsor 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.

您可以使用一些特殊的应用程序,例如BDV SystemEvents或任何其他应用程序。它允许您指定在其他应用程序关闭时将启动的应用程序。将相同的应用程序指定为条件和操作,您将获得预期的结果。