Script Sample 01

|^^|

source: http://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux

UTILIZATION COMMANDS NEEDED:

https://blogs.oracle.com/pranav/entry/how_to_find_out_cpu_utilizatio

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Introduction_To_System_Administration/s2-bandwidth-rhlspec-cpu.html

sar -u 2 5

pidstat 5 -p 2295

LOAD AVERAGE AND ANALOGY

http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages

top -bn1 | grep "Cpu(s)" | \ sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \ awk '{print 100 - $1"%"}'

3

7

4

1

3

Every time I run this command, I get the exact same output (32.7%). – alanaktion Jul 18 '13 at 1:58

A more accurate result is given when I use top -bn2, but it takes a long time. From what I've read, this seems to be the only way to get an accurate result. – alanaktion Jul 18 '13 at 2:10

top -bn1 seems wildly inaccurate on my FC20 system. top -bn2 seems to work well. – Carpetsmoker Feb 19 '14 at 0:58

In my server, i need change regex: "s/.*, ([0-9.]*)% -------> sed "s/.*: ([0-9.]*)%.*/\1/" – Edgard Leal Feb 21 '14 at 2:12

The command in this answer appears to be written for systems where top -v returns procps-ng (e.g., Fedora). There's also procps, found on, e.g., Ubuntu and CentOS, where the command doesn't work (always indicates 100%, because parsing fails due to the line with the CPU figures being formatted differently). Here's a version that works with both implementations: top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' -v prefix="$prefix" '{ split($1, vs, ","); v=vs[length(vs)]; sub("%", "", v); printf "%s%.1f%%\n", prefix, 100 - v }'mklement0 Feb 21 '14 at 4:15

Side note: on OSX, use the following: top -l 2 -n 0 -F | egrep -o ' \d*\.\d+% idle' | tail -1 | awk -F% -v prefix="$prefix" '{ printf "%s%.1f%%\n", prefix, 100 - $1 }'. – mklement0 Feb 21 '14 at 4:15

I use this using perl: top -b -n2 -p 1|perl -lane 'if( /Cpu(s):\s+([\d\.]+)%us,\s+([\d\.]+)%sy/){$u=$1 + $2;}END{printf("%.1f%%\n", $u);}' – sfgroups Aug 1 '14 at 22:49

@mklement0, does your command top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' -v prefix="$prefix" '{ split($1, vs, ","); v=vs[length(vs)]; sub("%", "", v); printf "%s%.1f%%\n", prefix, 100 - v }' give instant CPU usage or total average CPU usage since boot? – Sagar Jha Jan 12 '15 at 0:54

@SagarJha: Not since boot - "instant" in that the percentage reported is based on 2 current samples (1-2 secs. apart, depending on implementation) - apparently at least 2 samples are needed to get a somewhat accurate reading. – mklement0 Jan 12 '15 at 16:54

I found out that the only reliable way, even if it takes 1 second is to use mpstat: mpstat 1 1 | grep 'Average' | awk '{ printf "CPU: %.0f%%", 100-$NF }'idragosalex May 16 '15 at 10:31

So, when output of top -bn1 | grep "Cpu(s)" is %Cpu(s): 2,2 us, 0,6 sy, 0,0 ni, 96,XXX id, 0,6 wa, 0,1 hi, 0,0 si, 0,0 st so the percentage is 100 - XXX, right? – Fredrick Gauss Jul 27 '15 at 11:46

I use this: top -n1 -b | grep "Cpu(s):" | awk -F " " '{print $2}' | sed -e 's/us,//' – Krzysztof Jarosz Dec 16 '15 at 10:37

Take a look at cat /proc/stat

grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'

eof