Get per-core CPU usage in terminal in machine-readable format

bashcommand lineterminal

I need CPU utilization for each core. I want text-based output in the terminal. htop technically contains the information I need, but the output is not suitable for my use case as I need to pipe the output to another command for parsing. Is there a way to get this information in a more machine-readable text format?

EDIT: Any well known format would be nice, e.g. CSV/TSV, JSON, XML. Or something simple would be perfectly fine, like newline-delimited lines. Just something I can easily parse. I can possibly parse the htop output, as it is text. However, that feels wrong because it's sort of "graphical" looking and I can't be sure future versions won't change the look.

Best Answer

There are ways to get this information from htop, but they basically involve screen scraping, as it is designed primarily for interactive use.

Instead, check out the glances command-line tool, which can do what you want.

It can be installed via homebrew:

brew install glances

Something like the following may work for you:

glances --stdout now,percpu --time 10