DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Get Overall CPU Usage on Linux


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.



Published

Jun 30, 2014

Last Updated

Jun 30, 2014

Category

Tech

Tags

  • cpu 3
  • linux 158
  • top 3

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor