开发者

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 lo开发者_StackOverflow中文版oked at a lot of questions here, but none could help me. Any suggestions?


$ 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).


from shell, ps with the cp output format specifier:

ps -p <pid> -o cp

From python - see psutil.Process.get_cpu_percent()


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜