How to Get Percentage of Processor Use with Bash

How to get percentage of processor use with bash?

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

Retrieve CPU usage percentage

A simple awk could help you here(considering that you want to print only the numbers of sy column).

vmstat 1 10 | awk 'FNR>1{print $(NF-3)}'

NOTE: I have used vmstat 1 10 to perform 10 times vmstat command on server and then I am printing the $(NF-3) value which is 4th value from last.

How can I determine the current CPU utilization from the shell?

Linux does not have any system variables that give the current CPU utilization. Instead, you have to read /proc/stat several times: each column in the cpu(n) lines gives the total CPU time, and you have to take subsequent readings of it to get percentages. See this document to find out what the various columns mean.

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

You can use top and/or vmstat from the procps package.
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.

%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

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.)

This command prints every 5 seconds:

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%

Get CPU Usage from Windows Command Prompt

C:\> wmic cpu get loadpercentage
LoadPercentage
0

Or

C:\> @for /f "skip=1" %p in ('wmic cpu get loadpercentage') do @echo %p%
4%


Related Topics



Leave a reply



Submit