批处理脚本,用于监视和响应特定程序的CPU使用情况 [英] Batch script to monitor and react to CPU usage for a certain program

查看:62
本文介绍了批处理脚本,用于监视和响应特定程序的CPU使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否曾经问过这个问题,但是这里是:

I do not know if this question was asked before, but here goes:

是否有一种编写批处理脚本的方法,使得它可以连续监视某个可执行程序的cpu使用率%,直到cpu达到0%?假设我们有一个名为 xyz.exe 的程序,根据任务管理器,该程序当前使用约2-4%的cpu.一定时间后,CPU会达到0%.我尝试使用 tasklist 命令,如下所示,但无法针对cpu进行调整:

Is there a way to write a batch script such that it continuously monitors the cpu usage % of a certain executable program until the cpu hits 0%? Say we have a program called xyz.exe, which currently uses up about 2-4% of cpu according to the task manager. After a certain time the cpu hits 0%. I've tried using tasklist command as follows, but was unable to tweak it for cpu purposes:

@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

TYPEPERF LOGMAN

TYPEPERF ; LOGMAN

这篇关于批处理脚本,用于监视和响应特定程序的CPU使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆