如何通过 bash 获得处理器使用的百分比?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26791240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get percentage of processor use with bash?
提问by Filipi Silva
I wonder how do I get the percentage of my processor usage from 0% to 100%?
我想知道如何将处理器使用率从 0% 提高到 100%?
to know how many percent'm using my processor preferably in bash or other methods provided that percentage.
要知道有多少百分比正在使用我的处理器,最好是在 bash 或提供该百分比的其他方法中。
I have this script that I found on google however it is very much imprecisso I tried to make more improvements could not, does anyone know any method to get the percentage of CPU utilization in% 0-100
我有我在谷歌上找到的这个脚本,但是它非常不精确,我试图做更多的改进不能,有没有人知道任何方法来获得 0-100% 的 CPU 利用率百分比
my script
我的剧本
NUMCPUS=`grep ^proc /proc/cpuinfo | wc -l`; FIRST=`cat /proc/stat | awk '/^cpu / {print }'`; sleep 1; SECOND=`cat /proc/stat | awk '/^cpu / {print }'`; USED=`echo 2 k 100 $SECOND $FIRST - $NUMCPUS / - p | dc`; echo ${USED}% CPU Usage
回答by
top -bn1 | sed -n '/Cpu/p'
gives the following line
给出以下行
Cpu(s): 15.4%us, 5.3%sy, 0.0%ni, 78.6%id, 0.5%wa, 0.0%hi, 0.1%si, 0.0%st
You can pull any CPU field with the following will take the userCPU (us)
您可以使用以下命令拉取任何 CPU 字段将占用用户CPU (us)
top -bn1 | sed -n '/Cpu/p' | awk '{print }' | sed 's/..,//'
Output:
输出:
15.4%
If you want another field like systemCPU (sy) you can change the awk field from $2,
如果您想要另一个字段,如系统CPU (sy),您可以将 awk 字段从 $2 更改为
top -bn1 | sed -n '/Cpu/p' | awk '{print }' | sed 's/..,//'
Output:
输出:
5.3%
If you want other CPU:
如果你想要其他 CPU:
us: user CPU used by user processes
sy: system CPU used by system/kernel processes
ni: nice CPU used by processes that were reniced
id: idle CPU not used
wa: io wait Essentially idle CPU waiting on IO devices
hi: hardware irq CPU used to service hardware IRQs
si: software irq CPU used to service soft IRQs
st: steal time CPU time which the hypervisor dedicated (or ‘stole') for other guests in the system.
回答by David C. Rankin
Processor use or utilization is a measurement over time. One way to measure utilization in %
is by computation over two successive reads of /proc/stat
. A simple common bash script to compute the percentage is:
处理器使用或利用率是随时间推移的衡量标准。衡量 利用率的一种方法%
是通过计算 的两个连续读取/proc/stat
。计算百分比的一个简单的常见 bash 脚本是:
#!/bin/bash
# Read /proc/stat file (for first datapoint)
read cpu user nice system idle iowait irq softirq steal guest< /proc/stat
# compute active and total utilizations
cpu_active_prev=$((user+system+nice+softirq+steal))
cpu_total_prev=$((user+system+nice+softirq+steal+idle+iowait))
usleep 50000
# Read /proc/stat file (for second datapoint)
read cpu user nice system idle iowait irq softirq steal guest< /proc/stat
# compute active and total utilizations
cpu_active_cur=$((user+system+nice+softirq+steal))
cpu_total_cur=$((user+system+nice+softirq+steal+idle+iowait))
# compute CPU utilization (%)
cpu_util=$((100*( cpu_active_cur-cpu_active_prev ) / (cpu_total_cur-cpu_total_prev) ))
printf " Current CPU Utilization : %s\n" "$cpu_util"
exit 0
use/output:
使用/输出:
$ bash procstat-cpu.sh
Current CPU Utilization : 10
output over 5 iterations:
输出超过 5 次迭代:
$ ( declare -i cnt=0; while [ "$cnt" -lt 5 ]; do bash procstat-cpu.sh; ((cnt++)); done )
Current CPU Utilization : 20
Current CPU Utilization : 18
Current CPU Utilization : 18
Current CPU Utilization : 18
Current CPU Utilization : 18
回答by John1024
To get usage percent total since bringing the system up:
要获得系统启动后的总使用百分比:
awk '/cpu /{print 100*(+)/(++)}' /proc/stat
To get the usage percentage over the last second:
要获取最后一秒的使用百分比:
awk -v a="$(awk '/cpu /{print +,++}' /proc/stat; sleep 1)" '/cpu /{split(a,b," "); print 100*(+-b[1])/(++-b[2])}' /proc/stat
Explanation
解释
From man 5 proc
, the meaning of the first four numbers on the cpu line in /proc/stat
is given by:
从man 5 proc
,cpu 行中的前四个数字的含义/proc/stat
由下式给出:
cpu 3357 0 4313 1362393
The amount of time, measured in units of USER_HZ (1/100ths of a second on most architectures, use sysconf(_SC_CLK_TCK) to obtain the right value), that the system spent in user mode, user mode with low priority (nice), system mode, and the idle task, respectively. The last value should be USER_HZ times the second entry in the uptime pseudo-file.
cpu 3357 0 4313 1362393
系统在用户模式下花费的时间,以 USER_HZ 为单位(在大多数架构上为 1/100 秒,使用 sysconf(_SC_CLK_TCK) 获得正确的值),系统在用户模式下花费的时间,用户模式使用低优先级(nice)、系统模式和空闲任务。最后一个值应该是 USER_HZ 乘以正常运行时间伪文件中的第二个条目。
The get the CPU usage, we add the user and system times and divide by the total of user, system, and idle time.
获取 CPU 使用率,我们将用户和系统时间相加,然后除以用户、系统和空闲时间的总和。
Let's look again at the calculation for total CPU usage since system up:
让我们再看看系统启动后总 CPU 使用率的计算:
awk '/cpu /{print 100*(+)/(++)}' /proc/stat
By requiring that the line match cpu
, we get system totals. The second column is user time, the fourth is system time, and the fifth is idle time. The ratio is multiplied by 100 to get a percentage.
通过要求行匹配cpu
,我们得到系统总数。第二列是用户时间,第四列是系统时间,第五列是空闲时间。该比率乘以 100 得到一个百分比。
Now, let's consider the recent CPU usage:
现在,让我们考虑一下最近的 CPU 使用情况:
awk -v a="$(awk '/cpu /{print +,++}' /proc/stat; sleep 1)" '/cpu /{split(a,b," "); print 100*(+-b[1])/(++-b[2])}' /proc/stat
This reads /proc/cpu
twice, a second apart. The first time, the CPU user + system, and user+system+idle times are saved in the variable a
. sleep
is called to delay for a second. Then, /proc/cpu
is read a second time. Tne old user+system total is subtracted from the new total and divided by the change in the total of all times. The result is multiplied by 100 to convert it to percent and printed.
这读取/proc/cpu
两次,相隔一秒。第一次,CPU用户+系统,用户+系统+空闲时间保存在变量中a
。 sleep
被称为延迟一秒钟。然后,/proc/cpu
第二次阅读。从新的总数中减去旧的用户+系统总数并除以所有时间总数的变化。结果乘以 100 以将其转换为百分比并打印。
回答by Vess Bakalov
Very simple script that considers only System, Idle and User.
非常简单的脚本,只考虑系统、空闲和用户。
The benefit over the other answers is that it requires no utilities, not even top, and also displays fractions, which the current top answer does not.
相对于其他答案的好处是它不需要实用程序,甚至不需要顶部,并且还显示分数,而当前的顶部答案不需要。
#!/bin/bash
read u1 s1 i1 <<< $(grep 'cpu ' /proc/stat | awk '{print " "" "}' )
sleep 1
read u2 s2 i2 <<< $(grep 'cpu ' /proc/stat | awk '{print " "" "}' )
u=$(echo "scale=4;$u2-$u1" | bc)
s=$(echo "scale=4;$s2-$s1" | bc)
i=$(echo "scale=4;$i2-$i1" | bc)
cpu=$(echo "scale=4;($u+$s)*100/($u+$s+$i)" | bc)
echo $cpu
Brief description - we pull data from /proc/stat from the line that starts with 'cpu'. We then get parse out the second token which is user time, the fourth token which is system time and fifth token which is idle time.
简要说明 - 我们从 /proc/stat 以“cpu”开头的行中提取数据。然后我们解析出第二个令牌是用户时间,第四个令牌是系统时间,第五个令牌是空闲时间。
At this point, you may be tempted to just do the math, but all that will give you is the utilization since boot time. We need one more data point.
在这一点上,您可能只想做数学计算,但能给您带来的只是自启动时间以来的利用率。我们还需要一个数据点。
We sleep 1 second and we pull the data from /proc/stat again. Now we get the difference between the first pull and the second pull. This is the CPU utilization for that 1 second while we slept.
我们休眠 1 秒钟,然后再次从 /proc/stat 中提取数据。现在我们得到了第一次拉动和第二次拉动之间的差异。这是我们睡觉时那 1 秒的 CPU 利用率。
We get the difference for each of the variables, and then do the math on the difference. The strange 'scale=4' in front of each calculation is to force a float answer with 4 digit precision.
我们得到每个变量的差异,然后对差异进行数学计算。每次计算前面奇怪的“scale=4”是强制使用 4 位精度的浮点答案。