How do I get the percentage of cpu usage using bash?
Wondering how to get the percentage of my cpu usage from 0% to 100%?
to find out what percentage my cpu is using, preferably in bash or other methods provided the percentage is.
I have this script which I found on google, however it is very inconvenient I tried to make more improvements, donβt know, does anyone know any method to get the CPU utilization percentage 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
source to share
Processor usage or utilization is a measurement over time. One way to measure usage in %
is to calculate on two consecutive reads /proc/stat
. Simple generic bash script for calculating percentage:
#!/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
Usage / output:
$ bash procstat-cpu.sh
Current CPU Utilization : 10
output for 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
source to share
To get the total utilization rate since system startup:
awk '/cpu /{print 100*($2+$4)/($2+$4+$5)}' /proc/stat
To get the last second used percentage:
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 the man 5 proc
value of the first four numbers on the cpu line in /proc/stat
is determined by the expression:
cpu 3357 0 4313 1362393
Amount of time, measured in USER_HZ units (1 / 100ths of a second on most architectures, uses sysconf (_SC_CLK_TCK) to get the correct value) that the system spent in user mode, user mode low priority (good), system mode and the task of inaction, respectively. The last value must be USER_HZ times the second write during pseudo file uptime.
Get CPU usage, add user and system time, and divide by total users, system, and downtime.
Look again at the calculation of total CPU usage since system startup:
awk '/cpu /{print 100*($2+$4)/($2+$4+$5)}' /proc/stat
By requiring a string to match cpu
, we get the system totals. The second column is the user time, the fourth is the system time, and the fifth is the idle time. The coefficient is multiplied by 100 to get the percentage.
Now let's look at 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 is read /proc/cpu
twice, the second time. The first time user + system CPU as well as user + system + downtime is stored in a variable a
. sleep
called to delay for a second. Then it /proc/cpu
is read a second time. The total of the old user + the system 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 print it.
source to share
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 out any cpu field using the following: custom cpu (us)
top -bn1 | sed -n '/Cpu/p' | awk '{print $2}' | sed 's/..,//'
Output:
15.4%
If you want a different 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 need a different processor:
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.