htop icon indicating copy to clipboard operation
htop copied to clipboard

Option for process list acquisition and data dumping

Open BenBE opened this issue 5 years ago • 4 comments

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).

BenBE avatar Dec 09 '20 08:12 BenBE

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.

danie-dejager avatar Feb 13 '21 11:02 danie-dejager

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"
  • -d keeps the session headless — not even attached to current terminal, which allows doing this in scripts running without tty, and doesn't fight with -x setting wide terminal so htop will show long(er) command lines without truncating, and -y so more commands fit...
  • capture-pane -e flag preserves colors — you do get ANSI escapes in the output, but only for color changes, all in top-down left-to-right order; omit -e if you prefer B&W without any escapes.
    -J trims trailing whitespace and joins wrapped lines (which htop probably never emits); -p pipes to stdout (instead of internal tmux buffer) so we can redirect to a file.
  • Exercises to the reader: Replace OUT as you wish e.g. mktemp.
    If you use tmux elsewhere, you might need to name the session and DONE "channel" uniquely(?)

cben avatar May 13 '25 08:05 cben

may be try TERM=ansi77 htop -n 1 ?

fasterit avatar May 13 '25 09:05 fasterit

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).

cben avatar May 13 '25 12:05 cben