【问题标题】:Batch: Shutdown PC if a Program isn't Running?批处理:如果程序未运行,请关闭 PC?
【发布时间】:2013-10-29 13:33:34
【问题描述】:

是否有一个批处理命令可以检查程序是否正在运行,如果没有则关闭 PC?

【问题讨论】:

    标签: batch-file shutdown


    【解决方案1】:

    这是可能的,寻找正在运行的 Internet Explorer 的示例:

    @echo off
    
    tasklist /fi "imagename eq iexplore.exe" | find /I "iexplore.exe" > nul
    if errorlevel 1 goto SHUTDOWN
    
    echo IE is already running
    goto DONE
    
    :SHUTDOWN
    shutdown -s -f -t 0
    
    goto DONE
    
    :DONE
    exit
    

    编辑

    如果你想循环这个命令使用这个:

    @echo off
    :loop
    tasklist /fi "imagename eq iexplore.exe" | find /I "iexplore.exe" > nul
    if errorlevel 1 goto SHUTDOWN
    
    echo IE is already running
    goto DONE
    
    :SHUTDOWN
    shutdown -s -f -t 0
    
    goto DONE
    
    :DONE
    REM wait 5 sec
    ping 127.0.0.1 -n 2 -w 5000 > NUL
    goto loop
    

    【讨论】:

    • set SR=%~d0%~p0 是怎么回事?
    • Batch 的当前路径 ;)
    • 我在 Windows 8 上写这个,但将在 Windows 7 上执行它。现在总是说 IE 正在运行。 8 有区别吗?
    • 好的。我下午试试。我现在将尝试在 Win8.1 上使用不同的程序运行它(我需要它的实际可执行文件)
    • 这是我在 iexpress 中制作的提取器。我希望计算机在完成执行后关闭。解压后如果有其他关闭方法请告知
    【解决方案2】:

    只为一个班轮

    for /l %%# in (1 0 2) do (tasklist|find /i "program.exe">nul ||(shutdown -s -f -t 0 & exit /b))
    

    创建一个从 1 到 2 的循环,步长为 0,检查任务列表输出中是否有 program.exe。如果失败则运行关闭并退出。

    【讨论】:

      【解决方案3】:

      这里有一个小递归函数,它也应该做你想做的事。

      @echo off
      setlocal
      
      call :ShutDownIfNotRunning Process.exe
      exit /b
      
      :ShutDownIfNotRunning
      tasklist /FI "imagename eq %~1" | Findstr /i "%~1">nul
      if not errorlevel 1 ( 
      echo %~1 is running. & goto ShutDownIfNotRunning
      ) ELSE ( shutdown -s -f -t 0 )
      exit /b
      

      【讨论】:

        猜你喜欢
        • 2021-08-16
        • 2018-07-05
        • 1970-01-01
        • 2012-08-11
        • 1970-01-01
        • 2019-02-19
        • 2014-08-10
        • 2014-07-19
        • 1970-01-01
        相关资源
        最近更新 更多