使用taskkill打破无限循环

时间:2015-10-16 17:20:24

标签: batch-file

我尝试进行无限循环来检查node.exe是否正在使用超过一定数量的内存。像这样:

:loop2
sleep
taskkill /f /im node.exe "memusage gt 85000" > nul
timeout /t 30
GOTO loop2

但是,当处理taskkill时,如何打破这个无限循环?

5 个答案:

答案 0 :(得分:2)

set "Myprocess=node.exe"
:test
tasklist | find /i "%MyProcess%">nul  && Taskkill /F /IM  "%MyProcess%" & exit/b
timeout /t 30
goto:test

答案 1 :(得分:1)

EntityManager

检查命令的输出以确定进程是否已被终止。

已编辑我认为:loop2 sleep ( taskkill /f /im node.exe /fi "memusage gt 85000" | find "PID" >nul ) || ( timeout /t 30 GOTO loop2 ) 字符串与区域设置无关,但事实并非如此。如果需要,PID命令可以替换为

find

在输出中搜索进程的名称,或

find /i "node.exe" > nul

搜索进程ID的存在(注意:所有进程ID是4的倍数)

答案 2 :(得分:1)

适用于任何语言环境。

:loop2
sleep
set _tk=
for /f "skip=1 delims=, tokens=2" %%A in ('tasklist /fi "imagename eq node.exe" /fi "memusage gt 85000" /fo csv') do (
    taskkill /f /pid %%~A
    set _tk=1
)
if not "%_tk%"=="1" (
    timeout /t 30
    GOTO loop2
)

答案 3 :(得分:0)

另一种方法是使用TASKLIST查看匹配的图像是否正在运行。此代码未经过测试。

:loop2

SET TEMPFILE=%TEMP%\tk_%RANDOM%.tmp

TASKLIST /FI "IMAGENAME eq cmd.exe" /FI "MEMUSAGE gt 9000" >"%TEMPFILE%"
SET /P TKI=<"%TEMPFILE%"
IF "%TKI%" EQU "INFO: No tasks are running which match the specified criteria." (GOTO OutOfIt)

taskkill /f /im node.exe "memusage gt 85000" > nul
timeout /t 30
GOTO loop2

:OutOfIt
IF EXISTS "%TEMPFILE%" (DEL "%TEMPFILE%")
EXIT /B

答案 4 :(得分:0)

taskkill /IM notepad.exe /FI "memusage gt 999" |find "SUCCESS" &&GOTO :BREAKOUT