Run the following command to get the overall CPU usage from command line:
top -bn2|grep "Cpu(s)"|sed "s/.*, *\([0-9.]*\)%* *id.*/\1/"|awk '{print 100 - $1 "%"}'|sed -n 2p
You can see the result is extracted from "top" command. You can study this command section by section. The first step is top -bn2
, which print 2 frame of "top" command to console instead of to curses window.
Here we use "-bn2" instead of "-bn1" is because the first ouput is always inaccurate. See comments in How to get overall CPU Usage (e.g. 57%) on Linux for details.
The second step is top -bn2|grep "Cpu(s)"
, the 3rd step is top -bn2|grep "Cpu(s)"|sed "s/.*, *\([0-9.]*\)%* *id.*/\1/"
, etc.