我不知道以前是否有人问过这个问题,但这里是:
有没有办法编写一个批处理脚本,使其持续监控某个可执行程序的 cpu 使用率百分比,直到 cpu 达到 0%?假设我们有一个名为 xyz.exe
的程序,根据任务管理器,该程序当前使用了大约 2-4% 的 cpu。一段时间后,cpu 达到 0%。我试过如下使用 tasklist
命令,但无法针对 cpu 目的对其进行调整:
@echo off
:loop
tasklist | "xyz"
if errorlevel 1 (
echo xyz still running
goto loop
) else (
goto next
)
:next
xyz completed
最佳答案
使用 typeperf "\Process(xyz)\% Processor Time"
您可以检查 xyz
的处理器使用情况:
@echo off
:check
for /f "skip=2 tokens=2 delims=," %%c in ('typeperf "\Process(xyz)\%% Processor Time" -sc 1') do (
set cpu_usage=%%~c
goto :break
)
:break
echo %cpu_usage%
set cpu_usage=%cpu_usage:.=%
:: 1 is set in the front to avoid octal comparison
if 1%cpu_usage% LSS 11000000 (
goto :check_process
) else (
goto :check
)
:: sleep for 1 second
pathping 127.0.0.1 -n -q 1 -p 1000 >nul 2>&1
:check_process
QPROCESS * | find /i "xyz" >nul 2>&1 && (
echo process xyz is running
) || (
echo process xyz is not running
)
endlocal
关于windows - 用于监视某个程序的 CPU 使用率并对其使用react的批处理脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25215083/