1

I have found that htop command was the only way to display nicely each CPU core's load. I need an average of each CPU core from the last few seconds. However, I do not know how to extract those numbers in a command line. For example, using grep or -n 1. I am running Ångström where I am not allowed to install any more extra packages.

I would like my end result to be:

CPU0: X%
CPU1: X%

I would appreciate any help.

Gabriele
  • 737
  • 2
  • 8
  • 20
  • Possible duplicate of [How to get overall CPU Usage (e.g. 57%) on Linux](http://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux) – rm-vanda May 05 '17 at 15:04
  • I found the answer in this [link](https://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux/ "link") where it calculates accurate cpu given in percentage. – Gabriele May 09 '17 at 14:35

1 Answers1

2

Instead of htop which is more of a gui, go straight to /proc/stat and grab what you need.

Using awk:

awk '$1~/cpu[0-9]/{usage=($2+$4)*100/($2+$4+$5); print $1": "usage"%"}' /proc/stat
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • 4
    Unfortunately, it calculates the average since last reboot. I need an average of the last few seconds. How would I go about it? – Gabriele May 08 '17 at 10:39
  • `/proc/stat` info for anyone who is interested http://www.linuxhowtos.org/System/procstat.htm – jmarceli Aug 23 '18 at 13:29