Linux – cpu usage per process


How can I grab the percentage of cpu usage on a per process basis? So, for example, I'd like to run my program prog and get the cpu usage it incurred in, for example:

  prog name cpu0 cpu1 cpu2 cpu3  total
  prog        15   20   45   47   127%

Is there any tool for this?

Best Solution

For firefox:

while [ 1 ]; do ps --no-heading -C firefox -L -o command,psr,pcpu|sort -k 2 -n; echo; sleep 1; done

You'd have to sum the third column (which I see no ridiculously easy way to do) because it's actually showing you every thread. First column is name, second processor, third, %cpu.