是否有一个批处理命令可以检查程序是否正在运行,如果没有则关闭 PC?
问问题
1595 次
3 回答
1
只为一个班轮
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。如果它失败运行关闭并退出。
于 2013-10-29T15:38:02.633 回答
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
于 2013-10-29T13:35:56.457 回答
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
于 2013-10-29T14:47:46.973 回答