2

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?

2
  • 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? Oct 3, 2009 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, 2012 at 23:44

1 Answer 1

4

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 }'

4
  • 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\"%\" }"' Oct 3, 2009 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, 2009 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, 2009 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, 2015 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .