Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

batch file - How to convert a tasklist's CPU time to CPU % usage?

I'm trying to use tasklist to find out which process is consuming more than X percent of my CPU (to later kill it with taskkill.)

How do I know what percent a time format represents?

The documentations says:

TASKLIST options

/FI   filter               

And one filter may be:

CPUTIME         eq, ne, gt, lt, ge, le        CPU time in the format: hh:mm:ss.
                                              hh - number of hours,
                                              mm - minutes, ss - seconds

If I try

tasklist /FI "CPUTIME gt 00:00:10" 

it works.

But if I

tasklist /FI "CPUTIME gt 90"

it doesn't.

How can I know that time format represent 90%? Or 80%? What's the relationship between CPU usage time and the CPU usage percent?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It doesn't look like there's an easy way to do this with tasklist, so I would suggest either doing this in VBscript or another scripting language, or using a different approach. If you're constrained to batch files then you could use the WMIC command to get the list of running processes with their respective CPUTime:

C:> wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessorTime

Name                 PercentProcessorTime
Idle                 0
System               0
Smss                 0
csrss                0
winlogon             0
services             0
lsass                0

[...]

wmiprvse             100
wmic                 0
_Total               100

Note that this in my testing showed wmipsrv.exe as having 100% CPU, because it spiked while executing the WMI query. You should account for that in your script or you'll end up trying to kill the WMI service constantly ;)

Reference:
http://waynes-world-it.blogspot.com/2008/09/useful-general-command-line-operations.html
http://technet.microsoft.com/en-us/library/bb742610.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...