简体   繁体   English

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

[英]overall CPU usage and Memory(RAM) usage in percentage in linux/ubuntu

I want to findout overall CPU usage and RAM usage in percentage, but i dint get success我想按百分比找出总体 CPU 使用率和 RAM 使用率,但我没有成功

$ command for cpu usage
4.85%

$ command for memory usage
15.15%

OR或者

$ command for cpu and mamory usage
cpu: 4.85%
mem: 15.15%

How can I achieve this?我怎样才能做到这一点?

You can use top and/or vmstat from the procps package.您可以使用 procps 包中的top和/或vmstat Use vmstat -s to get the amount of RAM on your machine (optional), and then use the output of top to calculate the memory usage percentages.使用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 

You can also do this for relatively short output:您也可以为相对较短的输出执行此操作:

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

A shell pipe that calculates the current RAM usage periodically is定期计算当前 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 usages were filtered out here.) (此处过滤掉了 CPU + Swap 使用情况。)

This command prints every 5 seconds:此命令每 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%

Please use one of the following:请使用以下方法之一:

$ free -t | awk 'NR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
Current Memory Utilization is : 14.6715

OR或者

$ free -t | awk 'FNR == 2 {print "Current Memory Utilization is : " $3/$2*100}'
Current Memory Utilization is : 14.6703

CPU usage => top -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,' | awk '{print 100-$8 "%"}' CPU 使用率 => top -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,' | awk '{print 100-$8 "%"}' top -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,' | awk '{print 100-$8 "%"}'

Memory usage => free -m | grep 'Mem:' | awk '{ print $3/$2*100 "%"}' Memory 用法 => free -m | grep 'Mem:' | awk '{ print $3/$2*100 "%"}' free -m | grep 'Mem:' | awk '{ print $3/$2*100 "%"}'

For the cpu usage% you can use:对于 CPU 使用率,您可以使用:

top -b -n 1|顶部 -b -n 1| grep Cpu | grep CPU | awk -F "," '{print $4}' | awk -F "," '{print $4}' | awk -F "id" '{print $1}' | awk -F "id" '{print $1}' | awk -F "%" '{print $1}' awk -F "%" '{print $1}'

One liner solution to get RAM % in-use:一种获得 RAM % in-use 的线性解决方案:

free -t | awk 'FNR == 2 {printf("%.0f%"), $3/$2*100}'

Example output: 24%示例输出:24%

for more precision, you can change the integer N inside printf(%.<N>%) from the the previous command.为了更精确,您可以更改前一个命令中printf(%.<N>%)的整数 N。 For example to get 2 decimal places of precision you could do:例如,要获得 2 个小数位的精度,您可以执行以下操作:

free -t | awk 'FNR == 2 {printf("%.2f%"), $3/$2*100}'

Example output: 24.57%示例输出:24.57%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM