如何查看 Linux 下 CPU、内存和交换分区的占用率? | Linux 中国

找不到分类 evilven 5个月前 (05-22) 157次浏览 已收录 0个评论 扫描二维码
如何查看 Linux 下 CPU、内存和交换分区的占用率? | Linux 中国

在大多数情况下我们只想查看内存使用情况,并没有考虑占用的百分比究竟是多少。如果你想要了解这些信息,那你看这篇文章就对了。

— Vinoth Kumar


在 Linux 下有很多可以用来查看内存占用情况的命令和选项,但是我并没有看见关于内存占用率的更多的信息。

在大多数情况下我们只想查看内存使用情况,并没有考虑占用的百分比究竟是多少。如果你想要了解这些信息,那你看这篇文章就对了。我们将会详细地在这里帮助你解决这个问题。

这篇教程将会帮助你在面对 Linux 服务器下频繁的内存高占用情况时,确定内存使用情况。

而在同时,如果你使用的是 free -m 或者 free -g,占用情况描述地也并不是十分清楚。

这些格式化命令属于 Linux 高级命令。它将会对 Linux 专家和中等水平 Linux 使用者非常有用。

方法-1:如何查看 Linux 下内存占用率?

我们可以使用下面命令的组合来达到此目的。在该方法中,我们使用的是 free 和 awk 命令的组合来获取内存占用率。

如果你正在寻找其他有关于内存的文章,你可以导航到如下链接。这些文章有 free 命令[1]smem 命令[2]ps_mem 命令[3]vmstat 命令[4] 及 查看物理内存大小的多种方式[5]

要获取不包含百分比符号的内存占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'NR == 2 {print "Current Memory Utilization is : " $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">或</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'FNR == 2 {print "Current Memory Utilization is : " $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Memory</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">20.4194</span></code></section></li>
</ol>

要获取不包含百分比符号的交换分区占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'NR == 3 {print "Current Swap Utilization is : " $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">或</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'FNR == 3 {print "Current Swap Utilization is : " $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Swap</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0</span></code></section></li>
</ol>

要获取包含百分比符号及保留两位小数的内存占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'NR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">或</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'FNR == 2 {printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Memory</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">20.42</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span></code></section></li>
</ol>

要获取包含百分比符号及保留两位小数的交换分区占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">或</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'FNR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Swap</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.00</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span></code></section></li>
</ol>

如果你正在寻找有关于交换分区的其他文章,你可以导航至如下链接。这些链接有 使用 LVM(逻辑盘卷管理)创建和扩展交换分区[6]创建或扩展交换分区的多种方式[7] 和 创建/删除和挂载交换分区文件的多种方式[8]

键入 free 命令会更好地作出阐释:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">              total        used        free      shared  buff</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">/</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">cache   available</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Mem</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">          </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">15867</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">        </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">3730</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">        </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">9868</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">        </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">1189</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">        </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">2269</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">       </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">10640</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Swap</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">         </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">17454</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">           </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">       </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">17454</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Total</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">        </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">33322</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">        </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">3730</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">       </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">27322</span></code></section></li>
</ol>

细节如下:

◈ free:是一个标准命令,用于在 Linux 下查看内存使用情况。
◈ awk:是一个专门用来做文本数据处理的强大命令。
◈ FNR == 2:该命令给出了每一个输入文件的行数。其基本上用于挑选出给定的行(针对于这里,它选择的是行号为 2 的行)
◈ NR == 2:该命令给出了处理的行总数。其基本上用于过滤给出的行(针对于这里,它选择的是行号为 2 的行)
◈ $3/$2*100:该命令将列 3 除以列 2 并将结果乘以 100。
◈ printf:该命令用于格式化和打印数据。
◈ %.2f%:默认情况下,其打印小数点后保留 6 位的浮点数。使用后跟的格式来约束小数位。

方法-2:如何查看 Linux 下内存占用率?

我们可以使用下面命令的组合来达到此目的。在这种方法中,我们使用 freegrep 和 awk 命令的组合来获取内存占用率。

要获取不包含百分比符号的内存占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Mem</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'{print "Current Memory Utilization is : " $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Memory</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">20.4228</span></code></section></li>
</ol>

要获取不包含百分比符号的交换分区占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Swap</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'{print "Current Swap Utilization is : " $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Swap</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0</span></code></section></li>
</ol>

要获取包含百分比符号及保留两位小数的内存占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Mem</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'{printf("Current Memory Utilization is : %.2f%"), $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Memory</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">20.43</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span></code></section></li>
</ol>

要获取包含百分比符号及保留两位小数的交换空间占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ free </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">t </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Swap</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'{printf("Current Swap Utilization is : %.2f%"), $3/$2*100}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Swap</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.00</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span></code></section></li>
</ol>

方法-1:如何查看 Linux 下 CPU 的占用率?

我们可以使用如下命令的组合来达到此目的。在这种方法中,我们使用 topprint 和 awk 命令的组合来获取 CPU 的占用率。

如果你正在寻找其他有关于 CPU(LCTT 译注:原文误为 memory)的文章,你可以导航至如下链接。这些文章有 top 命令[9]htop 命令[10]atop 命令[11] 及 Glances 命令[12]

如果在输出中展示的是多个 CPU 的情况,那么你需要使用下面的方法。

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">top</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">b </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">n1 </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">^%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">5.3</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">94.7</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu1</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">100.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu2</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">94.7</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">5.3</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu3</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">5.3</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">94.7</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu4</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">10.5</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">15.8</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">73.7</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu5</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">5.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">95.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu6</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">5.3</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">94.7</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu7</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">5.3</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">94.7</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">  </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
</ol>

要获取不包含百分比符号的 CPU 占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">top</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">b </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">n1 </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">^%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'{cpu+=$9}END{print "Current CPU Utilization is : " 100-cpu/NR}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> CPU </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">21.05</span></code></section></li>
</ol>

要获取包含百分比符号及保留两位小数的 CPU 占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">top</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">b </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">n1 </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">^%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'{cpu+=$9}END{printf("Current CPU Utilization is : %.2f%"), 100-cpu/NR}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> CPU </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">14.81</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span></code></section></li>
</ol>

方法-2:如何查看 Linux 下 CPU 的占用率?

我们可以使用如下命令的组合来达到此目的。在这种方法中,我们使用的是 topprint/printf和 awk 命令的组合来获取 CPU 的占用率。

如果在单个输出中一起展示了所有的 CPU 的情况,那么你需要使用下面的方法。

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">top</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">b </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">n1 </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">^%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">(</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">s</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">):</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">15.3</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> us</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">7.2</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> sy</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.8</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> ni</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">69.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">id</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">6.7</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> wa</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> hi</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">1.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> si</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">,</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">0.0</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> st</span></code></section></li>
</ol>

要获取不包含百分比符号的 CPU 占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">top</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">b </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">n1 </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">^%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'{print "Current CPU Utilization is : " 100-$8}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> CPU </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">5.6</span></code></section></li>
</ol>

要获取包含百分比符号及保留两位小数的 CPU 占用率:

<ol class="linenums list-paddingleft-2" style="margin-left: 2em;margin-right: 2em;">
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">$ </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">top</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">b </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">-</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">n1 </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">grep</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">^%</span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Cpu</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">|</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> awk </span><span class="str" style="overflow-wrap: break-word;color: rgb(101, 176, 66);">'{printf("Current CPU Utilization is : %.2f%"), 100-$8}'</span></code></section></li>
<li><section style="overflow-wrap: break-word;width: 1200px;max-width: 1200px !important;"><code style="overflow-wrap: break-word;background: none;color: rgb(33, 150, 243);line-height: 1.2em;padding-left: 10px !important;border-radius: 0px !important;margin-top: 1em !important;margin-bottom: 1em !important;border-width: initial !important;border-style: none !important;border-color: initial !important;"><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Current</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> CPU </span><span class="typ" style="overflow-wrap: break-word;color: rgb(137, 189, 255);">Utilization</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="kwd" style="overflow-wrap: break-word;color: rgb(226, 137, 100);">is</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">:</span><span class="pln" style="overflow-wrap: break-word;color: rgb(184, 255, 184);"> </span><span class="lit" style="overflow-wrap: break-word;color: rgb(51, 135, 204);">5.40</span><span class="pun" style="overflow-wrap: break-word;color: rgb(184, 255, 184);">%</span></code></section></li>
</ol>

如下是一些细节:

◈ top:是一种用于查看当前 Linux 系统下正在运行的进程的非常好的命令。
◈ -b:选项允许 top 命令切换至批处理的模式。当你从本地系统运行 top 命令至远程系统时,它将会非常有用。
◈ -n1:迭代次数。
◈ ^%Cpu:过滤以 %CPU 开头的行。
◈ awk:是一种专门用来做文本数据处理的强大命令。
◈ cpu+=$9:对于每一行,将第 9 列添加至变量 cpu
◈ printf:该命令用于格式化和打印数据。
◈ %.2f%:默认情况下,它打印小数点后保留 6 位的浮点数。使用后跟的格式来限制小数位数。
◈ 100-cpu/NR:最终打印出 CPU 平均占用率,即用 100 减去其并除以行数。

via: https://www.2daygeek.com/linux-check-cpu-memory-swap-utilization-percentage/

作者:Vinoth Kumar[14] 选题:lujun9972 译者:An-DJ 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

如何查看 Linux 下 CPU、内存和交换分区的占用率? | Linux 中国


Hacking For Fun , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何查看 Linux 下 CPU、内存和交换分区的占用率? | Linux 中国
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址