I want to monitor the CPUs' usage when a program is running. I came out with the idea, that htop has the CPU usage information on the top. I wonder if I can somehow run htop, make it refresh every 0.1 second, and output the information about CPU usage to a file. Does anyone have an idea how to achieve that? Any help would be deeply appreciated.
1 Answer
I ended up using sar command and wrote a short script so that it keeps running until ctrl+c stops it.
-
Doesn't, for instance,
sar 1
already keep it running with a 1 sec interval until you hit ^c? What more do you need in a script?– doneal24Commented Aug 24, 2022 at 19:44 -
sar
,atop
, orbelow
are for -- don't reinvent the wheel by trying to parse something which was never designed to be parsed.top -b
(batch mode) outputs plain text (no colours or ncurses escapes), which is reasonably easy to parse.