How to use the top command in Unix to only see process using more than 10% CPU?

05
2014-04
  • jake9115

    I want to use top, but not see all of the processes that are using minute chunks of computing power. Can someone tell me how to use top to see things above a certain cpu % cutoff? I tried just using vanilla top that ranks processes by CPU usage and pass it through head, but it doesn't refresh: top | head -n 15

    Thanks!

  • Answers
  • Daniel Beck

    On OS X top (which should be similar to BSD top), you can run top -o cpu -n 10 to display the top 10 processes by CPU usage.

  • terdon

    I don't see the point of what you're trying to do since top sorts by CPU% by default so all you need is to resize your terminal window to only have the top processes showing.

    The GNU top I have on my Debian has no option for this in man top, but you could always run something convoluted like:

    watch  "top -bn1 | awk '{if(/^[^0-9 ]/){print}else if(\$9 >= 10){print}}' "
    

    This uses watch which will run the specified command every two seconds (by default, change the interval with -n) and gawk to parse the output of top in batch mode and only print the processes using at least 10% CPU.


  • Related Question

    linux - average load and total %CPU in top
  • Tim

    I thought the average load by uptime and the summation of %CPU of all running processes in top (#9 column) should agree. But it seems not true. Here is my little experiments:

    On one server:

    $ top -b -n 1| awk '{ totuse = totuse + $9 } END { print totuse/100 }'; uptime
    
    6.29
    
    22:00:59 up 28 days,  7:03,  9 users,  load average: 7.03, 5.81, 4.51`
    

    On another server:

    $ top -b -n 1| awk '{ totuse = totuse + $9 } END { print totuse/100 }'; uptime
    
    4.93
    
    22:01:37 up 29 days,  8:27, 17 users,  load average: 18.83, 16.01, 13.86`
    

    So why is there such a difference between the two? Which one more truly reflect the usage of CPUs?

    If I try to evaluate how much CPU usage my running processes are using, is this a good way:

    top -b -n 1 | grep "tim"| awk '{ totuse = totuse + $9 } END { print totuse/100 }'
    

    ?

    Thanks and regards!


  • Related Answers
  • baumgart

    The reason your CPU % and load average are not agreeing is because they are two completely different values. The CPU % is exactly that, the percentage of the CPU a process is using. The load average is the weighted average of the processes in the run queue over 1, 5, and 15 minutes.

    If you are concerned with how much CPU you are using (are you fully utilizing your CPU), your tally of the output of top will work well. You can run that occasionally and record the value (or use sar, which will do that for you).

    Having a high load average means you have a lot of processes in the run queue - many processes are ready and waiting to run. High load doesn't automatically mean a lot of CPU usage.

    Wikipedia has a good article describing the load average, and the differences between CPU load and CPU usage: http://en.wikipedia.org/wiki/Load_Average