批处理脚本有条件地重新启动应用程序

这篇博客介绍了一个可自定义的批处理脚本,用于在应用程序挂起、无响应或需要始终保持运行状态时自动重启。通过设置不同条件,可以轻松双击或用热键重启应用,或者作为计划任务运行,确保应用程序始终运行。这个简单脚本避免了额外后台程序的需求。
摘要由CSDN通过智能技术生成

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/

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值