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
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.
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.
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:use/output:
output over 5 iterations:
gives the following line
You can pull any CPU field with the following will take the user CPU (us)
Output:
If you want another field like system CPU (sy) you can change the awk field from $2,
Output:
If you want other CPU:
To get usage percent total since bringing the system up:
To get the usage percentage over the last second:
Explanation
From
man 5 proc
, the meaning of the first four numbers on the cpu line in/proc/stat
is given by: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:
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:
This reads
/proc/cpu
twice, a second apart. The first time, the CPU user + system, and user+system+idle times are saved in the variablea
.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.