This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
看一下
cat /proc/stat
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {打印用法“%”}'
编辑请在复制粘贴之前阅读评论或将其用于任何严肃的工作。这没有经过测试或使用,对于那些不想安装实用程序或不想在任何发行版中运行的东西的人来说,这是一个想法。有些人认为你可以“apt-get install”任何东西。
注意:这不是当前 CPU 使用率,而是自系统启动以来所有内核的总体 CPU 使用率。这可能与当前的 CPU 使用情况有很大不同。要获取当前值,必须使用 top (或类似工具)。
当前 CPU 使用率可以通过以下方式计算:
Take a look at
cat /proc/stat
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
EDIT please 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.
NOTE: this is not the current CPU 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.
Current CPU usage can be potentially calculated with:
您可以尝试:
You can try:
尝试使用
sysstat
包中的mpstat
然后进行一些
cut
或grep
来解析您需要的信息:Try
mpstat
from thesysstat
packageThen some
cut
orgrep
to parse the info you need:不妨用我的解决方案提出一个实际的响应,该解决方案受到 Peter Liljenberg 的启发:
这将使用 awk 打印出 100 减去第 12 个字段(空闲),并在其后面带有百分号。
awk
只会对第 12 个字段仅包含数字和点的行执行此操作 ($12 ~ /[0-9]+/
)。您还可以平均五个样本,间隔一秒:
像这样测试:
Might as well throw up an actual response with my solution, which was inspired by Peter Liljenberg's:
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]+/
).You can also average five samples, one second apart:
Test it like this:
编辑:我注意到在另一个用户的回复中 %idle 是字段 12 而不是字段 11。 awk 已更新以考虑到 %idle 字段是可变的。
这应该会得到您想要的输出:
如果您想要简单的整数舍入,您可以使用 printf:
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.
This should get you the desired output:
If you want a simple integer rounding, you can use printf:
执行此操作可以查看整体 CPU 使用率。这会调用
python3
并使用跨平台psutil
模块< /a>.interval=2
部分表示测量 2 秒阻塞期内的总 CPU 负载。示例输出:
它包含的 python 程序是这样的:
将
time
放在调用前面证明在这种情况下它需要大约 2 秒的指定间隔时间。以下是调用和输出:要查看各个内核的输出,让我们使用下面的 python 程序。首先,我获得“每个 CPU”信息的 python 列表(数组),然后对该列表中的所有内容进行平均以获得“总 % CPU”类型值。然后我打印总数和单个核心百分比。
Python 程序:
如果您愿意,可以将其包装成一个极其丑陋的单行 bash 脚本。我必须确保在 Python 程序中仅使用单引号 (
''
),而不是双引号 (""
),以便将其包装到 bash 1 -liner 工作:示例输出:请注意,我有 8 个核心,因此“Individual CPUs:”后面有 8 个数字:
有关如何
psutil.cpu_percent(interval=2) 的更多信息
蟒蛇调用有效,请参阅官方psutil.cpu_percent(interval=None, percpu=False)
此处的文档:更进一步:
我在 cpu_logger.py 中使用上述代码我的 eRCAGuy_dotfiles 存储库。
参考资料:
相关
Do this to see the overall CPU usage. This calls
python3
and uses the cross-platformpsutil
module.The
interval=2
part says to measure the total CPU load over a blocking period of 2 seconds.Sample output:
The python program it contains is this:
Placing
time
in front of the call proves it takes the specified interval time of about 2 seconds in this case. Here is the call and output:To view the output for individual cores as well, let's use this python program below. First, I obtain a python list (array) of "per CPU" information, then I average everything in that list to get a "total % CPU" type value. Then I print the total and the individual core percents.
Python program:
This can be wrapped up into an incredibly ugly 1-line bash script like this if you like. I had to be sure to use only single quotes (
''
), NOT double quotes (""
) in the Python program in order to make this wrapping into a bash 1-liner work:Sample output: notice that I have 8 cores, so there are 8 numbers after "Individual CPUs:":
For more information on how the
psutil.cpu_percent(interval=2)
python call works, see the officialpsutil.cpu_percent(interval=None, percpu=False)
documentation here:Going further:
I use the above code in my cpu_logger.py script in my eRCaGuy_dotfiles repo.
References:
Related