Common system and/or environmental events such as resuming from standby or losing network connection can cause problems for certain applications which expect to be always on and connected. So if you have a certain application which crashes or goes into “not responding” mode somewhat frequently and a restart is the only fix for it, we have a simple fix for you in the form of a customizable batch script to simply kill the application and restart it.
常见的系统和/或环境事件(例如,从待机状态恢复或网络连接丢失)会给某些希望始终打开并连接的应用程序带来问题。 因此,如果您的某个应用程序经常崩溃或进入“不响应”模式,而重新启动是它的唯一解决方案,那么我们可以通过可自定义的批处理脚本的形式为您提供一个简单的修复程序,以简单地杀死该应用程序并重新启动它。
In addition to the obvious situation above, this script can be used for a variety of useful tasks, such as:
除了上述明显的情况之外,此脚本还可以用于各种有用的任务,例如:
- Easily restart an application by double-clicking or using a hot key. 双击或使用热键轻松重新启动应用程序。
- Restart a program only when it is hung or not responding. 仅在程序挂起或没有响应时才重新启动它。
- Run as a scheduled task to make sure an application is always running. 作为计划任务运行,以确保应用程序始终在运行。
- Anywhere else you want to automate conditional restarting of an application. 您希望自动执行应用程序有条件重启的其他任何地方。
Customizing the script should be pretty self explanatory by the comments, so just configure the script appropriately and you are all set.
通过注释自定义脚本应该很容易解释,因此只需适当地配置脚本即可。
剧本 (The Script)
@ECHO OFF
ECHO Restart Application
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.
SETLOCAL EnableExtensions
REM Enter the application information.
SET AppName=Application Name
SET ExeFile=FileToLaunch.exe
SET ExePath=C:PathToApplication
REM Select the conditions to kill the application.
REM A value of 1 = Yes, 0 = No
SET KillIfRunning=1
SET KillIfNotResponding=1
SET KillIfUnknownStatus=1
REM Specify when to start the application:
REM 1 = Start only if the process was previous killed.
REM 0 = Start the application regardless.
SET StartOnlyIfKilled=1
SET KillStatus="%TEMP%KillStatus.tmp.txt"
SET Success=0
ECHO Killing existing %AppName% instance...
IF {%KillIfRunning%}=={1} CALL :CheckKillStatus "%ExeFile%" "RUNNING"
IF {%KillIfNotResponding%}=={1} CALL :CheckKillStatus "%ExeFile%" "NOT RESPONDING"
IF {%KillIfUnknownStatus%}=={1} CALL :CheckKillStatus "%ExeFile%" "UNKNOWN"
ECHO.
IF {%StartOnlyIfKilled%}=={1} (
IF {%Success%}=={0} GOTO End
)
ECHO Restarting %AppName%...
START "%ExeFile%" "%ExePath%%ExeFile%"
ECHO.
IF EXIST %KillStatus% DEL /F /Q %KillStatus%
ENDLOCAL
:CheckKillStatus
ECHO Killing with status: %~2
TASKKILL /FI "STATUS eq %~2" /IM "%~1" /F > %KillStatus%
SET /P KillResult= < %KillStatus%
FOR /F "tokens=1,* delims=:" %%A IN ("%KillResult%") DO (
ECHO %%A:%%B
IF /I {%%A}=={SUCCESS} SET /A Success=%Success%+1
)
:End
结论(Conclusion)
While you can find applications out there which perform basically the same function, using a simple script such as this avoids having “yet another program” running in the background.
尽管您可以在其中找到执行基本相同功能的应用程序,但是使用这样的简单脚本可以避免在后台运行“另一个程序”。
翻译自: https://www.howtogeek.com/50630/batch-script-to-conditionally-restart-an-application/