Linux 如何从 shell 确定当前的 CPU 利用率?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1332861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:38:55  来源:igfitidea点击:

How can I determine the current CPU utilization from the shell?

linux

提问by Joel

How can I determine the current CPU utilization from the shell in Linux?

如何从 Linux 中的 shell 确定当前的 CPU 利用率?

For example, I get the load average like so:

例如,我得到这样的平均负载:

cat /proc/loadavg

Outputs:

输出:

0.18 0.48 0.46 4/234 30719

采纳答案by Martin v. L?wis

Linux does not have any system variables that give the current CPU utilization. Instead, you have to read /proc/statseveral times: each column in the cpu(n)lines gives the total CPU time, and you have to take subsequent readings of it to get percentages. See this documentto find out what the various columns mean.

Linux 没有任何提供当前 CPU 利用率的系统变量。相反,您必须/proc/stat多次阅读:行中的每一列都cpu(n)给出了总 CPU 时间,您必须随后阅读它以获得百分比。请参阅此文档以了解各个列的含义。

回答by elcuco

You need to sample the load average for several seconds and calculate the CPU utilization from that. If unsure what to you, get the sources of "top" and read it.

您需要对平均负载采样几秒钟,然后从中计算 CPU 利用率。如果您不确定什么,请获取“顶部”的来源并阅读它。

回答by Johan

Maybe something like this

也许像这样

ps -eo pid,pcpu,comm

And if you like to parse and maybe only look at some processes.

如果你喜欢解析,也许只看一些过程。

#!/bin/sh
ps -eo pid,pcpu,comm | awk '{if ( > 4) print }' >> ~/ps_eo_test.txt

回答by Space

You can use topor pscommands to check the CPU usage.

您可以使用topps命令来检查 CPU 使用率。

using top : This will show you the cpu stats

使用 top :这将显示 CPU 统计信息

top -b -n 1 |grep ^Cpu

using ps: This will show you the % cpu usage for each process.

using ps: 这将显示每个进程的 cpu 使用百分比。

ps -eo pcpu,pid,user,args | sort -r -k1 | less

Also, you can write a small script in bash or perl to read /proc/stat and calculate the CPU usage.

此外,您可以用 bash 或 perl 编写一个小脚本来读取 /proc/stat 并计算 CPU 使用率。

回答by Denes Tarjan

Try this command:

试试这个命令:

cat /proc/stat

This will be something like this:

这将是这样的:

cpu  55366 271 17283 75381807 22953 13468 94542 0
cpu0 3374 0 2187 9462432 1393 2 665 0
cpu1 2074 12 1314 9459589 841 2 43 0
cpu2 1664 0 1109 9447191 666 1 571 0
cpu3 864 0 716 9429250 387 2 118 0
cpu4 27667 110 5553 9358851 13900 2598 21784 0
cpu5 16625 146 2861 9388654 4556 4026 24979 0
cpu6 1790 0 1836 9436782 480 3307 19623 0
cpu7 1306 0 1702 9399053 726 3529 26756 0
intr 4421041070 559 10 0 4 5 0 0 0 26 0 0 0 111 0 129692 0 0 0 0 0 95 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 369 91027 1580921706 1277926101 570026630 991666971 0 277768 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 8097121
btime 1251365089
processes 63692
procs_running 2
procs_blocked 0

More details:

更多细节:

http://www.mail-archive.com/[email protected]/msg01690.htmlhttp://www.linuxhowtos.org/System/procstat.htm

http://www.mail-archive.com/[email protected]/msg01690.html http://www.linuxhowtos.org/System/procstat.htm

回答by Aaron Digulla

The command uptimegives you load averages for the past 1, 5, and 15 minutes.

该命令uptime为您提供过去 1、5 和 15 分钟的平均负载。