【发布时间】:2013-10-29 13:33:34
【问题描述】:
是否有一个批处理命令可以检查程序是否正在运行,如果没有则关闭 PC?
【问题讨论】:
标签: batch-file shutdown
是否有一个批处理命令可以检查程序是否正在运行,如果没有则关闭 PC?
【问题讨论】:
标签: batch-file shutdown
这是可能的,寻找正在运行的 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 是怎么回事?
只为一个班轮
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。如果失败则运行关闭并退出。
【讨论】:
这里有一个小递归函数,它也应该做你想做的事。
@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
【讨论】: