htop output to human readable file

扶醉桌前 提交于 2019-11-28 08:11:10

htop author here.

No, there's no "nice" way to get the output of htop piped into a file. It is an interactive application and uses terminal redraw routines to produce its interface (therefore, piping it makes as much sense as, for example, piping vim into a text file -- you'll get similar results).

To get the information about your processes in a text format, use "ps". For example, ps auxf > file.txt gives you lots of easy to parse information (or ps aux if you do not wish tree-formatting -- see man ps for more options).

htop outputs ANSI escape code to use colors and move the cursor around the terminal. There is a great command line program aha that can be used to convert ANSI into HTML.

Ubuntu/Debian installation

apt-get install aha

Save htop output as HTML file

echo q | htop | aha --black --line-fix > htop.html

I have had the same need, and ended up using top instead of htop a is provides a batch mode via the -b flag.

-b : Batch mode operation Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.

So for example:

top -b -n 1

Hope this helps even if this is not using htop.

k0fe

This command outputs plain text. (It requires installing aha and html2text.)

echo q | htop -C | aha --line-fix | html2text -width 999 | 
grep -v "F1Help\|xml version=" > file.txt

You can also use script prior to running htop in a mode that will redirect timings to a file for later playback. In the realm of 'yet another work around' and 'good for show and tell'.

script -t -a /var/tmp/script.htop.out 2> /var/tmp/script.htop.out.timings
htop 

Then to playback

scriptreplay /var/tmp/script.htop.out.timings /var/tmp/script.htop.out
Ian

Install recode first, then encode it to utf-8:

$htop | recode utf-8 > test.txt

Then cat the file and you should be good.

This may sound really noobish, however, if you have multiple monitors you could have htop running while "record my desktop" is capturing that area. Its more of a video and may not help with searching and sorting but it would look nice and pretty.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!