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
    That's what tools like sar, atop, or below are for -- don't reinvent the wheel by trying to parse something which was never designed to be parsed.
    – Chris Down
    Aug 24, 2022 at 18:14
  • @ChrisDown Great, sar is exactly what I should actually use, thanks a lot! Aug 24, 2022 at 19:32
  • Using top -b (batch mode) outputs plain text (no colours or ncurses escapes), which is reasonably easy to parse. Aug 24, 2022 at 21:26

1 Answer 1


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?
    – doneal24
    Aug 24, 2022 at 19:44
  • oh, that is even better, thnaks. Aug 28, 2022 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .