Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to figure out what the CPU usage of Firefox is at any given moment in time, and pipe that number to another script.

So, top is not the answer, since its output is in a chart and polluted with special characters.

I've also tried:

ps -eo pid,cp | grep $(pidof firefox)

But that gives CPU usage divided by CPU time, which is not the same that top or Gkrellm report.

So, how can I write a one-liner that gets the CPU usage of a particular PID?

share|improve this question
    
I'm not sure I understand your question. For me, ps and top show similar numbers. Are you running top with IRIX mode on or off? Can you show examples of how the output differs? What distro? How many cores? –  Dennis Williamson Oct 3 '09 at 18:01
    
Do you need CPU time or CPU usage as a percentage of a core (or the whole system)? –  Jodie C Mar 7 '12 at 23:44

1 Answer 1

up vote 3 down vote accepted

Top can provide the information, and with some creative awk/grep usage, you can pull out the pieces you want.

top -b -n 1 | grep firefox | awk '{ totuse = totuse + $9 } END { print totuse }'

share|improve this answer
    
as baumgart mentioned you can add a watch to your command like this: watch -n 1 'top -b -n 1 |awk "/firefox-bin/{ cpu+=\$9 } END { print \"firefox cpu usage: \" cpu\"%\" }"' –  mezgani Oct 3 '09 at 19:36
    
This line prints 0 even if Firefox isn't running: top -b -n 1 | grep firefox | perl -ne 'printf "%s\n", int((split /\s+/, $_)[8]); –  Neil Oct 3 '09 at 20:33
    
It prints 0 because it's summing values. If you want just one process (and you know there will only be one), you can shrink that perl stuff to awk '{print $9}'. The full command becomes top -b -n 1 | grep firefox | awk '{print $9}'. –  baumgart Oct 4 '09 at 14:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.