0

I can't obtain CPU% usage of all the pid, without know any program names.

I feel I am close to the solution, this is what I've done so far:

for line in $(pgrep -f chrome); \
   do echo -n $line" - ";       \ 
   ps -p $line -o %cpu | sed -n 2p | sed 's/ //'; done

In this example I obtain only all chrome pid.. in next step I want all executing pid.

Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146

2 Answers2

0

You can do this easily with the top command alone.

To order by CPU percentage (descending), you could use top -o -cpu

ktm5124
  • 11,861
  • 21
  • 74
  • 119
0

If you don't want to use for some reason, couple of other ways I can think of doing this.

> ps -e -o "%p-%C" 

Or if you wanted to do it in a script, something like (alternatively could just parse again or check /proc/pid/stat for cpu usage)

#!/bin/bash
shopt -s extglob
for line in /proc/+([0-9]); do
  echo -n "${line##*/}- "
  ps -p "${line##*/}" -o %cpu | sed -n 2p | sed 's/ //'
done

Where

  • shopt -s extglob Turns on extended file globing in
  • +([0-9]) Matches any files containing 1 or more digits
  • ${line##*/} Strips everything before and including the last / character
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48