Option for process list acquisition and data dumping
There should be some option for htop to be run in a "CLI one-shot" mode, that only gathers all data for the process list (and maybe meters?) and dumps them to stdout in a machine-readable format (bonus points, if its also human-readable).
The dump content provided may depend on some flavour given; like basic (only shown columns), full (gather all columns, even ones normally disabled when not shown), extended (like full, but also including internal data like highlighting offsets).
I logged a similar request. I'd like to consolidate all the software I use for monitoring and troubleshooting. It would make a big difference if I can run htop and have it output to file, on each run, so I can pull out what I need. This output would be understood by all the admins and have a familiar look as most of the guys use htop.
Better even if there are filters like htop --manual CPU that would print out just the CPU portion of htop for that moment.
Cool! The output in -n1 mode still uses a lot of escape sequences to jump around the screen (?), which makes it hard to post-process.
- one reason I wanted to post-process is the output takes a full-height screen even if had few processes (e.g. with
--filter).
So here is a recipe to use tmux to interpret the escape sequences and dump the final picture as linear [plain] text:
rm OUT
tmux new-session -d -x 500 -y 300 'htop -n1; tmux capture-pane -e -J -p > OUT; tmux wait-for -S DONE'
tmux wait-for DONE
# Now post-processing is easy. Remove trailing spaces and empty lines:
perl -0pe 's/ *$//mg; s/\n*\Z/\n/' < "$OUT"
-dkeeps the session headless — not even attached to current terminal, which allows doing this in scripts running without tty, and doesn't fight with-xsetting wide terminal so htop will show long(er) command lines without truncating, and-yso more commands fit...- capture-pane
-eflag preserves colors — you do get ANSI escapes in the output, but only for color changes, all in top-down left-to-right order; omit-eif you prefer B&W without any escapes.
-Jtrims trailing whitespace and joins wrapped lines (which htop probably never emits);-ppipes to stdout (instead of internal tmux buffer) so we can redirect to a file. - Exercises to the reader: Replace
OUTas you wish e.g.mktemp.
If you use tmux elsewhere, you might need to name the session andDONE"channel" uniquely(?)
may be try TERM=ansi77 htop -n 1 ?
afaict TERM=ansi77 drops colors and uses less unicode, but if you pipe to | cat -A you'll see it still uses various cursor-positioning escapes.
Which is fine if you simply dump the result to a terminal, but it matters for post-processing. For example, if I pipe to | grep '%' I don't only get the CPU usage lines, I also get pretty much the whole screen because instead of emitting newlines, ncurses decided to move cursor, so grep sees in single long line.
It's fine decision imho that htop -n1 still uses curses to paint, reusing all the existing code. It even has benefits when used with a terminal, such as honoring screen size and $TERM!
With tmux it can paint however it wants and I can grep etc. however I want, win-win.
P.S. I now looked at procps-ng top -n1 output — I'd thought it just prints plain-text but turns out it also uses many escape sequences (and is size & $TERM sensitive).