Display CPU cores usage in percentage without using top command Display CPU cores usage in percentage without using top command shell shell

Display CPU cores usage in percentage without using top command


From reading the man page for /proc/stat and this how to page, the numbers represent...

...the amount of time the CPU has spent performing different kinds of work. Time units are in USER_HZ or Jiffies (typically hundredths of a second).

With this knowledge, I believe creating the percentages you want are pretty straight forward. Using the output you gave us, here's how you would do this. However, keep in mind that these numbers are going to be an aggregate over the entire time since the computer's/server's last reboot.

/proc/stat file output:

cpu0 2473973 90 524817 769734476 73628 1124 158588 0 0cpu1 2199709 103 307315 774486870 68723 95 13171 0 0

Some simple math

First add total elapsed time units since bootup.

 772966696 = 2473973 + 90 + 524817 + 769734476 + 73628 + 1124 + 158588 + 0 + 0

Now calculate those percentages

cpu0: 2473973/772966696  90/772966696  524817/772966696  769734476/772966696 ...

And format the output

cpu0: 0.3%us, 0.0%sy, 0.0%ni, 99.5% id ...

Let me know if you need help coding this up but it shouldn't be any harder than reading the file, pulling out the lines you want, splitting the line by space and doing the math above. Hopefully this makes sense and feel free to ask me any followup questions.