Parag Gupta
Parag Gupta

Reputation: 117

CPU Utilization on UNIX

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?

Upvotes: 1

Views: 558

Answers (3)

Amber
Amber

Reputation: 526763

$ ps o "pid cp"
  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
  2

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

Upvotes: 3

Ofir
Ofir

Reputation: 8362

from shell, ps with the cp output format specifier:

ps -p <pid> -o cp

From python - see psutil.Process.get_cpu_percent()

Upvotes: 1

Dan Bizdadea
Dan Bizdadea

Reputation: 1302

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

Upvotes: 0

Related Questions