批处理:如果程序未运行,请关闭PC?

时间:2013-10-29 13:33:34

标签: batch-file shutdown

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

仅限一个班轮

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

创建一个从1开始到2的循环,步长为0,检查tasklist输出中是否有program.exe。如果失败则运行shutdown并退出。

答案 2 :(得分:0)

这是一个小的递归函数,也应该做你想要的。

@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