【问题标题】:Batch Detecting Program Not Responding批量检测程序无响应
【发布时间】:2018-12-27 01:12:53
【问题描述】:

我正在尝试编写一个批处理脚本来检测 .exe 是否没有响应,如果没有响应,它将运行一段代码来杀死它,然后还会执行其他一些操作。我知道如果进程没有在一行中响应,我可以如何终止进程并重新启动它,但我不确定我能做的不仅仅是通过将其转换为 if 语句或调用 goto 来重新启动它。

taskkill /im "exeName.exe" /fi "STATUS eq NOT RESPONDING" /f >nul && start "" "pathToExe"

我遇到过其他类似的 Stack Overflow 帖子,但是它们只检查进程的错误级别,而不检查程序是否没有响应以及之后如何执行代码。

我该怎么做呢?提前致谢。

【问题讨论】:

  • @KenY-N 不检查任务是否没有响应。这仅检查是否存在其他错误,例如当前是否未运行。
  • 你想要的问题是“如果没有响应,那么除了终止进程之外做其他事情”?

标签: batch-file


【解决方案1】:

假设我正确地解释了你的问题,并且你想在不终止任务的情况下进行测试,那么这个怎么样:

tasklist /fi "status eq not responding" /nh | find "exeName.exe" >NUL
if %errorlevel% == 0 (
    echo Oops, we've hung...
)

tasklist 采用与taskkill 相同的/fi status 选项,即使文档声明只允许RUNNING - Windows 8 上的taskkill /? 至少显示完整选项。然后我们使用find 来检查可执行文件是否在未响应任务列表中。

顺便说一句,如果你想改用PowerShell

$foo = get-process exeName
if (!$foo.Responding) {
    echo "Oops, we've hung..."
}

【讨论】:

    【解决方案2】:

    可以更轻松地完成:

    taskkill /FI "STATUS eq NOT RESPONDING" /IM "yourexe.exe" /F | findstr /c:"SUCCESS" >nul
    if %errorlevel% EQU 0 (echo Successfully detected and terminated "yourexe.exe" which didn't respond) else (echo Ooops! We didn't find or could not terminate process "yourexe.exe")
    

    如果您只想检测进程是否没有响应,请使用tasklist

    tasklist /FI "STATUS eq NOT RESPONDING" | findstr /c:"yourexe.exe">nul
    if %errorlevel% EQU 0 (echo We have detected that process with image name "yourexe.exe" is not responding.) else (echo We didn't found process with image name "yourexe.exe" because it doesn't exist.)
    

    在这两种情况下,我们都使用findstr 命令,因为即使进程未找到/终止taskkill/tasklist 也会返回errorlevel 0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多