I am wondering how you can get the system CPU usage and present it in percent using bash, for example.
Sample output:
57%
In case there is more than one core, it would be nice if an average percentage could be calculated.
I am wondering how you can get the system CPU usage and present it in percent using bash, for example.
Sample output:
57%
In case there is more than one core, it would be nice if an average percentage could be calculated.
Take a look at
cat /proc/stat
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'
EDIT please read comments before copy-paste this or using this for any serious work. This was not tested nor used, it's an idea for people who do not want to install a utility or for something that works in any distribution. Some people think you can "apt-get install" anything.
Might as well throw up an actual response with my solution, which was inspired by Peter Liljenberg's:
This will use
awk
to print out 100 minus the 12th field (idle), with a percentage sign after it.awk
will only do this for a line where the 12th field has numbers and dots only ($12 ~ /[0-9]+/
).EDITED: I noticed that in another user's reply %idle was field 12 instead of field 11. The awk has been updated to account for the %idle field being variable.
This should get you the desired output:
If you want a simple integer rounding, you can use printf:
You can try:
Try
mpstat
from thesysstat
packageThen some
cut
orgrep
to parse the info you need: