Linux – How to query CPU usage for a process in Linux at a specific moment in time

cpu-usagelinux

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?

Best Answer

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