整体 CPU 使用率和内存 (RAM) 使用率 linux/ubuntu
overall CPU usage and Memory(RAM) usage in percentage in linux/ubuntu
我想了解总体 CPU 使用率和 RAM 使用率(百分比),但我没有成功
$ command for cpu usage
4.85%
$ command for memory usage
15.15%
或
$ command for cpu and mamory usage
cpu: 4.85%
mem: 15.15%
我怎样才能做到这一点?
您可以使用 procps 包中的 top
and/or vmstat
。
使用 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
对于相对较短的输出,您也可以这样做:
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/'"
(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%
请使用以下其中一项:
$ free -t | awk 'NR == 2 {print "Current Memory Utilization is : " /*100}'
Current Memory Utilization is : 14.6715
或
$ free -t | awk 'FNR == 2 {print "Current Memory Utilization is : " /*100}'
Current Memory Utilization is : 14.6703
对于cpu usage%,您可以使用:
顶部-b-n 1| grep Cpu | awk -F "," '{print $4}' | awk -F "id" '{print $1}' | awk -F "%" '{print $1}'
获得 RAM 使用率的线性解决方案:
free -t | awk 'FNR == 2 {printf("%.0f%"), /*100}'
示例输出:24%
为了更精确,您可以更改上一个命令中 printf(%.<N>%)
中的整数 N。例如,要获得小数点后 2 位的精度,您可以这样做:
free -t | awk 'FNR == 2 {printf("%.2f%"), /*100}'
示例输出:24.57%
CPU 用法 => top -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,' | awk '{print 100- "%"}'
内存使用情况 => free -m | grep 'Mem:' | awk '{ print /*100 "%"}'
我想了解总体 CPU 使用率和 RAM 使用率(百分比),但我没有成功
$ command for cpu usage
4.85%
$ command for memory usage
15.15%
或
$ command for cpu and mamory usage
cpu: 4.85%
mem: 15.15%
我怎样才能做到这一点?
您可以使用 procps 包中的 top
and/or vmstat
。
使用 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
对于相对较短的输出,您也可以这样做:
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/'"
(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%
请使用以下其中一项:
$ free -t | awk 'NR == 2 {print "Current Memory Utilization is : " /*100}'
Current Memory Utilization is : 14.6715
或
$ free -t | awk 'FNR == 2 {print "Current Memory Utilization is : " /*100}'
Current Memory Utilization is : 14.6703
对于cpu usage%,您可以使用:
顶部-b-n 1| grep Cpu | awk -F "," '{print $4}' | awk -F "id" '{print $1}' | awk -F "%" '{print $1}'
获得 RAM 使用率的线性解决方案:
free -t | awk 'FNR == 2 {printf("%.0f%"), /*100}'
示例输出:24%
为了更精确,您可以更改上一个命令中 printf(%.<N>%)
中的整数 N。例如,要获得小数点后 2 位的精度,您可以这样做:
free -t | awk 'FNR == 2 {printf("%.2f%"), /*100}'
示例输出:24.57%
CPU 用法 => top -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,' | awk '{print 100- "%"}'
内存使用情况 => free -m | grep 'Mem:' | awk '{ print /*100 "%"}'