linux/ubuntu中的总体CPU使用率和内存(RAM)使用率百分比

Har*_*dja 2 linux terminal command-line ubuntu-14.04

我想以百分比的形式找出总体CPU使用率和RAM使用率,但我得到了成功

$ command for cpu usage
4.85%

$ command for memory usage
15.15%
Run Code Online (Sandbox Code Playgroud)

要么

$ command for cpu and mamory usage
cpu: 4.85%
mem: 15.15%
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

knb*_*knb 5

您可以使用top和/或vmstat来自procps包.使用vmstat -s让您的计算机(可选)上的RAM量,然后用的输出top来计算内存使用百分比.

%Cpu(s):  3.8 us,  2.8 sy,  0.4 ni, 92.0 id,  1.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 24679620 total,  1705524 free,  7735748 used, 15238348 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 16161296 avail Mem 
Run Code Online (Sandbox Code Playgroud)

您也可以以相对较短的输出执行此操作:

watch '/usr/bin/top -b | head -4 | tail -2'

定期计算当前RAM使用情况的shell管道

watch -n 5 "/usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf(\"used: %s   total: %s  => RAM Usage: %.1f%%\", \$F[7], \$F[3], 100*\$F[7]/\$F[3]) if /KiB Mem/'"
Run Code Online (Sandbox Code Playgroud)

(这里过滤掉了CPU + Swap用法.)

此命令每5秒打印一次:

Every 5.0s: /usr/bin/top -b | head -4 | tail -2 | perl -anlE 'say sprintf("u...  wb3: Wed Nov 21 13:51:49 2018

used: 8349560   total: 24667856  => RAM Usage: 33.8%
Run Code Online (Sandbox Code Playgroud)