I'd like to periodically get snapshot of per-core utilization to save to file, basically same information as htop. However, htop is interactive so it's not friendly to scripting, is there an alternative way of getting this information?
3 Answers
A number of options are discussed on our sister site at Get per-core CPU load in shell script
Perhaps the most appropriate is mpstat
e.g.
$ mpstat -u -P ALL 1 1
Linux 4.15.0-23-generic (t400s) 2018-07-26 _x86_64_ (2 CPU)
12:40:13 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
12:40:14 PM all 3.50 0.00 1.50 0.50 0.00 0.00 0.00 0.00 0.00 94.50
12:40:14 PM 0 5.00 0.00 1.00 1.00 0.00 0.00 0.00 0.00 0.00 93.00
12:40:14 PM 1 2.02 0.00 1.01 1.01 0.00 0.00 0.00 0.00 0.00 95.96
Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
Average: all 3.50 0.00 1.50 0.50 0.00 0.00 0.00 0.00 0.00 94.50
Average: 0 5.00 0.00 1.00 1.00 0.00 0.00 0.00 0.00 0.00 93.00
Average: 1 2.02 0.00 1.01 1.01 0.00 0.00 0.00 0.00 0.00 95.96
It isn't easy to get the information directly from htop
for the reasons described here: htop output to human readable file

- 136,215
- 21
- 243
- 336
-
Problem is, this doesn't tell you all the things htop does -- which cores are utilised, what threads are they executing. – Peter Prographo Jan 26 '20 at 16:55
You can get per core utilization easily with dstat
as in
dstat --noupdate --output cpustats.csv -c -C 0,1,2,3 60
--noupdate disables intermediate updates when the delay specified is > 1 as in this case.
--output cpustats.csv outputs the results to a comma seperated value file of the name provided that you can import into calc or your favorite spreadsheet program. leave this out for onscreen output like shown below.
-c selects cpu output only
-C 0,1,2,3 reports on cores 0 through 3
the 60 at the end is a delay in seconds between readings.
You are free to adjust these parameters (and more) to obtain the exact output you are looking for.
dstat is easily installable after enabling the Universe repository if you don't have it already with the command sudo apt install dstat
Sample output:
-------cpu0-usage--------------cpu1-usage--------------cpu2-usage--------------cpu3-usage------
usr sys idl wai hiq siq:usr sys idl wai hiq siq:usr sys idl wai hiq siq:usr sys idl wai hiq siq
3 1 95 0 0 0: 4 1 95 0 0 0: 3 1 95 1 0 0: 3 1 93 2 0 0
74 3 21 2 0 0: 74 3 17 5 0 0: 72 4 11 12 0 1: 73 3 20 3 0 0
Further info is available via man dstat

- 36,023
- 25
- 98
- 183
-
-
@PeterPrographo Welcome to AskUbuntu! Feel free to ask a new question referring to this question and specifying why these answers don't fill the bill (to avoid closure as a duplicate question) – Elder Geek Jan 26 '20 at 20:09
Consider sysstat
(in the normal repos)
It gives top
-like info upon request. Here is a sample of the utilization part
12:00:01 AM CPU %user %nice %system %iowait %steal %idle
12:05:01 AM all 0.87 0.00 0.84 0.17 0.00 98.12
12:15:01 AM all 0.78 0.00 0.81 0.17 0.00 98.24
12:25:01 AM all 0.77 0.00 0.81 0.19 0.00 98.22
12:35:01 AM all 0.77 0.00 0.82 0.19 0.00 98.23
12:45:01 AM all 1.99 0.35 0.97 0.27 0.00 96.41
12:55:01 AM all 0.78 0.00 0.79 0.16 0.00 98.27
It also logs memory use and traffic through the interfaces.
It's fairly configurable to meet your needs.
Good writeup here.

- 23,641
- 15
- 70
- 122
-
how can I show what threads cores are executing, like htop does? – Peter Prographo Jan 26 '20 at 16:56