How do I get the average CPU percentage (for every second) consumed by a single process since it starts until it ends (exists)? Could I make this with Top?


2 Answers 2


You can use the ps utility for this.

To see the lifetime CPU usage percentage of a single process, you can do:

ps h -p $PID -o %cpu 

To see the CPU percentage of all running processes, you can do something like:

ps ax -o pid,%cpu,comm

A way of doing it is to write a small Bash script that:

  • takes the process/command name of interest as sole argument, $arg1
  • extracts the PID for that process with an adequate ps | grep or a ps | awk
  • keeps the PID-number in $arg2
  • runs a cron job on the second with an executable based on a
    ps -eo pcpu,cmd | egrep $arg1 >> logfile-$arg2
  • conditions the cron job's termination to whether the process still exists, (just test for the PID number you stored in $arg2)
  • exits when the test PID does not show up anymore in the output of ps

You must log in to answer this question.

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