【问题标题】:Retrieve CPU usage percentage检索 CPU 使用百分比
【发布时间】:2018-11-27 07:25:46
【问题描述】:

我想检索处理器使用百分比,但没有其他显示大量数字的命令。有一个使用 'grep' 或 'awk' 或类似的解决方案。

~$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0 1447984 190232 146508 4983236  0    0     0    10    0    0  2  1 97  0

我使用了 vmstat 命令,我只想要 cpu 冒号中的 'sy' 号。

top command output :
    [m20176 libvirt-  20   0 4368m 4,0g 3028 S  12,0 25,3  24695:44 kvm               (B[m[39;49m

(B[m25320 libvirt-  20   0 3520m 3,0g 3056 S   2,7 19,2  21786:15 kvm               (B[m[39;49m

(B[m    1 root      20   0 10656  624  596 S   0,0  0,0   5:46.26 init              (B[m[39;49m
(B[m    2 root      20   0     0    0    0 S   0,0  0,0   0:00.12 kthreadd          (B[m[39;49m
(B[m    3 root      20   0     0    0    0 S   0,0  0,0 476:10.20 ksoftirqd/0       (B[m[39;49m
(B[m    6 root      rt   0     0    0    0 S   0,0  0,0   0:08.16 migration/0       (B[m[39;49m
(B[m    7 root      rt   0     0    0    0 S   0,0  0,0   2:03.06 watchdog/0        (B[m[39;49m
(B[m    8 root      rt   0     0    0    0 S   0,0  0,0   0:04.30 migration/1       (B[m[39;49m
(B[m   10 root      20   0     0    0    0 S   0,0  0,0   0:38.83 ksoftirqd/1       (B[m[39;49m
(B[m   12 root      rt   0     0    0    0 S   0,0  0,0   1:43.93 watchdog/1        (B[m[39;49m
(B[m   13 root      rt   0     0    0    0 S   0,0  0,0   0:03.41 migration/2       (B[m[39;49m
(B[m   15 root      20   0     0    0    0 S   0,0  0,0   2:42.22 ksoftirqd/2       (B[m[39;49m
(B[m   16 root      rt   0     0    0    0 S   0,0  0,0   1:49.23 watchdog/2        (B[m[39;49m
(B[m   17 root      rt   0     0    0    0 S   0,0  0,0   0:04.42 migration/3       (B[m[39;49m
(B[m   19 root      20   0     0    0    0 S   0,0  0,0 408:06.08 ksoftirqd/3       (B[m[39;49m

此处显示每个进程的进程使用情况(仅显示一部分)。我找到了这个命令:

`top -b -d1 -n1|grep -i "Cpu(s)"|head -c21|cut -d ' ' -f3|cut -d '%' -f1`

here。但这是启动时的 CPU 使用率,而不是实时使用率。

【问题讨论】:

标签: linux awk grep command cpu-usage


【解决方案1】:

一个简单的awk 可以帮助您(考虑到您只想打印 sy 列的数字)。

vmstat 1 10 | awk 'FNR>1{print $(NF-3)}'

注意: 我已经使用vmstat 1 10 在服务器上执行了 10 次 vmstat 命令,然后我打印了 $(NF-3) 值,它是第 4 个值最后。

【讨论】:

  • 非常感谢,它通过一些编辑解决了我的问题:vmstat 1 2 | awk 'FNR>3{print $(NF-3)}' 只得到一个数字并删除“sys”字
  • @NecroMeerkat,很酷,很高兴它帮助了你,欢呼并继续学习,继续分享。
猜你喜欢
  • 2014-06-02
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
相关资源
最近更新 更多