Take the 2-minute tour ×

I'm trying to calculate the percentage of CPU% used for a particular process using Python/Shell, but so far nothing.

I have looked at a lot of questions here, but none could help me. Any suggestions?

share|improve this question
Is top good enough for your purposes? –  Eamorr Sep 19 '11 at 11:19
This is a hard question to answer without context. What are you trying to accomplish? Are you hoping to figure out whether your program has balanced IO and CPU utilization optimally? Are you trying to defend your program against some network engineer's accusation that it is a CPU hog? When your tag mentions UNIX, which **nix variant do you mean? –  Ollie Jones Sep 19 '11 at 11:19
@Eamorr : Can't use top. Been told not to. –  Parag Gupta Sep 19 '11 at 11:21
@Ollie Jones: I am trying to make a system which would log the CPU utilization for my main program every minute. I'm using a RedHat server. –  Parag Gupta Sep 19 '11 at 11:23

3 Answers 3

up vote 1 down vote accepted

from shell, ps with the cp output format specifier:

ps -p <pid> -o cp

From python - see psutil.Process.get_cpu_percent()

share|improve this answer
I'm not sure what command to execute for that. I tried ps -cp <pid> –  Parag Gupta Sep 19 '11 at 11:18
$ ps o "pid cp"
 1609   0
 1813   0
 1851   0
 1885   0
 1896   0
 3164   0
21679   0
24019   2

If the process you're looking for might not be running as the same user etc, you can use ps -eo "pid cp" to show all processes on the system instead of just the default self-owned ones.

If you know a specific process ID, you could just do this:

$ ps -p 24019 -o "cp" --no-heading

where 24019 is the process ID you want, and the result you get is a 3-character string (right-aligned, potentially 3-digit number).

share|improve this answer
For some reason, it's showing only 4 processes. And not the one I want to monitor. –  Parag Gupta Sep 19 '11 at 11:23
...probably because that process belongs to different user. Check updated answer. +1 –  Andrejs Cainikovs Sep 19 '11 at 11:24

well, you can try to use the top command with "-b -n 1" and grab it's contents and than you can use cut or other tools to get what you need

NOTE: you can add the -p option to limit to a particular process id

share|improve this answer

Your Answer


