如何在终端(Mac)中获得以%为单位的CPU利用率 [英] How to get CPU utilization in % in terminal (mac)

查看:277
本文介绍了如何在终端(Mac)中获得以%为单位的CPU利用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux和Windows上看到了相同的问题,但在Mac(终端)上却没有看到.谁能告诉我如何以%为单位获取当前处理器利用率,所以示例输出为40%.谢谢

Ive seen the same question asked on linux and windows but not mac (terminal). Can anyone tell me how to get the current processor utilization in %, so an example output would be 40%. Thanks

推荐答案

这在Mac上有效(包括%):

This works on a Mac (includes the %):

ps -A -o %cpu | awk '{s+=$1} END {print s "%"}'

对此进行细分:

ps是过程状态工具.大多数* nix操作系统都支持它.我们要传递一些标志:

ps is the process status tool. Most *nix like operating systems support it. There are a few flags we want to pass to it:

  • -A表示所有进程,而不仅仅是您正在运行的进程.
  • -o让我们指定所需的输出.在这种情况下,我们只需要ps输出的cpu%列即可.
  • -A means all processes, not just the ones running as you.
  • -o lets us specify the output we want. In this case, it all we want to the cpu% column of ps's output.

这将为我们提供所有使用cpu的进程的列表,例如

This will get us a list of all of the processes cpu usage, like


0.0
1.3
27.0
0.0

我们现在需要将这个列表加起来以获得最终的数字,因此我们将ps的输出通过管道传送到awk. awk是用于解析和操作文本的功能非常强大的工具.我们只需要简单地将数字加起来,然后打印出结果,然后在末尾添加一个%"即可.

We now need to add up this list to get a final number, so we pipe ps's output to awk. awk is a pretty powerful tool for parsing and operating on text. We just simply add up the numbers, then print out the result, and add a "%" on the end.

这篇关于如何在终端(Mac)中获得以%为单位的CPU利用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆