How to get percentage of processor use with bash?

2019-04-07 16:20发布

I wonder how do I get the percentage of my processor usage from 0% to 100%?

to know how many percent'm using my processor preferably in bash or other methods provided that percentage.

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

my script

NUMCPUS=`grep ^proc /proc/cpuinfo | wc -l`; FIRST=`cat /proc/stat | awk '/^cpu / {print $5}'`; sleep 1; SECOND=`cat /proc/stat | awk '/^cpu / {print $5}'`; USED=`echo 2 k 100 $SECOND $FIRST - $NUMCPUS / - p | dc`; echo ${USED}% CPU Usage

4条回答
三岁会撩人
2楼-- · 2019-04-07 16:35

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 $2" "$4" "$5}' )
sleep 1
read u2 s2 i2 <<< $(grep 'cpu ' /proc/stat | awk '{print $2" "$4" "$5}' )
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.

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.

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.

查看更多
小情绪 Triste *
3楼-- · 2019-04-07 16:39

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:

#!/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:

$ ( 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
查看更多
叼着烟拽天下
4楼-- · 2019-04-07 16:40
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 user CPU (us)

top -bn1 | sed -n '/Cpu/p' | awk '{print $2}' | sed 's/..,//'

Output:

15.4%

If you want another field like system CPU (sy) you can change the awk field from $2,

top -bn1 | sed -n '/Cpu/p' | awk '{print $3}' | sed 's/..,//'

Output:

5.3%

If you want other 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.
查看更多
迷人小祖宗
5楼-- · 2019-04-07 16:42

To get usage percent total since bringing the system up:

awk '/cpu /{print 100*($2+$4)/($2+$4+$5)}' /proc/stat

To get the usage percentage over the last second:

awk -v a="$(awk '/cpu /{print $2+$4,$2+$4+$5}' /proc/stat; sleep 1)" '/cpu /{split(a,b," "); print 100*($2+$4-b[1])/($2+$4+$5-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:

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.

The get the CPU usage, we add the user and system times and divide by the total of user, system, and idle time.

Let's look again at the calculation for total CPU usage since system up:

awk '/cpu /{print 100*($2+$4)/($2+$4+$5)}' /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.

Now, let's consider the recent CPU usage:

 awk -v a="$(awk '/cpu /{print $2+$4,$2+$4+$5}' /proc/stat; sleep 1)" '/cpu /{split(a,b," "); print 100*($2+$4-b[1])/($2+$4+$5-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.

查看更多
登录 后发表回答