10

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
OscarRyz
  • 196,001
  • 113
  • 385
  • 569

3 Answers3

18

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

Jay
  • 41,768
  • 14
  • 66
  • 83
  • Is there a way to get a sorted by PercentProcessorTime output? – laggingreflex Sep 04 '14 at 06:48
  • 1
    Yes, you can filter it and do a bit of sorting like this, but the sort is not perfect: `wmic path Win32_PerfFormattedData_PerfProc_Process where "PercentProcessorTime > 1" get PercentProcessorTime, Name | sort /+20` – Amit Naidu Apr 19 '18 at 05:41
2

tasklist does not tell you how long the process has been running for. It tells you how much CPU time the various processes have taken up. To get a percentage (and that will be an average percentage) you need to know how long your process has been running for. You could for instance take snapshots 10 seconds apart, subtract the times, then find the process CPUTIME which is nearest to the number 10. (Well 10 times the number of CPU cores? not sure...)

SlightlyKosumi
  • 701
  • 2
  • 8
  • 24
-4

Tasklist's CPUTime is a measure of how much CPU time (cycles) have been used since the start of the process, so to convert that to a percent, it would be

 (TotalProcessRuntime / CpuTime) / 100

At least, thats what I gather :)

FlySwat
  • 172,459
  • 74
  • 246
  • 311
  • 3
    Could you elaborate on this? I can get the full list of processes with `CpuTime` using `tasklist /v` but how can I get `TotalProcessRuntime`? – Dziad Borowy Sep 11 '13 at 10:10
  • cpu_time is given in hh:mm:ss so convert it to seconds. Then add them all up for a totalProcessRuntime. Then you can loop through and do the division. – Steve Lloyd Aug 12 '19 at 13:32