如何在 Linux 上获得整体 CPU 使用率(例如 57%)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9229333/
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
How to get overall CPU usage (e.g. 57%) on Linux
提问by user1199739
I am wondering how you can get the system CPU usage and present it in percent using bash, for example.
例如,我想知道如何获取系统 CPU 使用率并使用 bash 以百分比表示。
Sample output:
示例输出:
57%
In case there is more than one core, it would be nice if an average percentage could be calculated.
如果有多个核心,如果可以计算平均百分比就好了。
回答by Peter Svensson
Try mpstat
from the sysstat
package
mpstat
从sysstat
包中尝试
> sudo apt-get install sysstat
Linux 3.0.0-13-generic (ws025) 02/10/2012 _x86_64_ (2 CPU)
03:33:26 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
03:33:26 PM all 2.39 0.04 0.19 0.34 0.00 0.01 0.00 0.00 97.03
Then some cut
or grep
to parse the info you need:
然后一些cut
或grep
解析您需要的信息:
mpstat | grep -A 5 "%idle" | tail -n 1 | awk -F " " '{print 100 - $ 12}'a
回答by vimdude
Take a look at cat /proc/stat
看一眼 cat /proc/stat
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
EDITplease read comments before copy-paste this or using this for any serious work. This was not tested nor used, it's an idea for people who do not want to install a utility or for something that works in any distribution. Some people think you can "apt-get install" anything.
编辑请在复制粘贴或将其用于任何严肃的工作之前阅读评论。这没有经过测试或使用,对于不想安装实用程序或在任何发行版中都可以使用的东西的人来说,这是一个想法。有些人认为你可以“apt-get install”任何东西。
NOTE:this is not the currentCPU usage, but the overall CPU usage in all the cores since the system bootup. This could be very different from the current CPU usage. To get the current value top (or similar tool) must be used.
注意:这不是当前的CPU 使用率,而是自系统启动以来所有内核的总体 CPU 使用率。这可能与当前的 CPU 使用率非常不同。要获取当前值,必须使用 top(或类似工具)。
Current CPU usage can be potentially calculated with:
当前 CPU 使用率可以通过以下方式计算:
awk '{u=+; t=++; if (NR==1){u1=u; t1=t;} else print (+-u1) * 100 / (t-t1) "%"; }' \
<(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)
回答by netcoder
You can try:
你可以试试:
top -bn1 | grep "Cpu(s)" | \
sed "s/.*, *\([0-9.]*\)%* id.*//" | \
awk '{print 100 - "%"}'
回答by jordanm
EDITED: I noticed that in another user's reply %idle was field 12 instead of field 11. The awk has been updated to account for the %idle field being variable.
编辑:我注意到在另一个用户的回复中 %idle 是字段 12 而不是字段 11。awk 已更新以说明 %idle 字段是可变的。
This should get you the desired output:
这应该会得到你想要的输出:
mpstat | awk ' ~ /CPU/ { for(i=1;i<=NF;i++) { if ($i ~ /%idle/) field=i } } ~ /all/ { print 100 - $field }'
If you want a simple integer rounding, you can use printf:
如果你想要一个简单的整数舍入,你可以使用 printf:
mpstat | awk ' ~ /CPU/ { for(i=1;i<=NF;i++) { if ($i ~ /%idle/) field=i } } ~ /all/ { printf("%d%%",100 - $field) }'
回答by Dan Fego
Might as well throw up an actual response with my solution, which was inspired by Peter Liljenberg's:
不妨对我的解决方案做出实际回应,这是受到 Peter Liljenberg 的启发:
$ mpstat | awk ' ~ /[0-9.]+/ { print 100 - "%" }'
0.75%
This will use awk
to print out 100 minus the 12th field (idle), with a percentage sign after it. awk
will only do this for a line where the 12th field has numbers and dots only ($12 ~ /[0-9]+/
).
这将用于awk
打印出 100 减去第 12 个字段(空闲),其后带有一个百分号。awk
只会对第 12 个字段只有数字和点 ( $12 ~ /[0-9]+/
)的行执行此操作。
You can also average five samples, one second apart:
您还可以平均间隔一秒的五个样本:
$ mpstat 1 5 | awk 'END{print 100-$NF"%"}'
Test it like this:
像这样测试它:
$ mpstat 1 5 | tee /dev/tty | awk 'END{print 100-$NF"%"}'