Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 4 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
@baumgart but this prints the cpu usage of the process over the life time and not for a given time interval like 1 sec or so. – whokares Dec 11 '15 at 20:54

Your Answer


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.