34

I've tried piping htop to a text file (e.g. htop > text.txt) but it gives me text garbled by formatting strings (see below). Is there a way to get nicer, human readable output?

^[7^[[?47h^[[1;30r^[[m^[[4l^[[?1h^[=^[[m^[[?1000h^[[m^[[m^[[H^[[2J^[[1B  ^[[36m1  ^[[m^[[1m[^[[m^[[32m||||||||||^[[31m||||||||||^[[30m^[[1m                                                            \
     22.2%^[[m]^[[m     ^[[36mTasks: ^[[1m159^[[m^[[36m total, ^[[32m^[[1m5^[[m^[[36m running^[[3;3H2  ^[[m^[[1m[^[[30m                                                                                \
      0.0%^[[m]^[[m     ^[[36mLoad average: ^[[30m^[[1m1.11 ^[[m^[[m1.28 ^[[1m1.31 ^[[4;3H^[[m^[[36m3  ^[[m^[[1m[^[[m^[[32m||||||||||^[[30m^[[1m                                                       \
                    11.1%^[[m]^[[m     ^[[36mUptime: ^[[1m9 days, 22:04:51^[[5;3H^[[m^[[36m4  ^[[m^[[1m[^[[30m                                                                                      0.0\
%^[[m]^[[6;3H^[[m^[[36m5  ^[[m^[[1m[^[[m^[[31m||||||||||^[[30m^[[1m                                                                           11.1%^[[m]^[[7;3H^[[m^[[36m6  ^[[m^[[1m[^[[30m           \
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119

8 Answers8

70

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

Hisham H M
  • 6,398
  • 1
  • 29
  • 30
  • 5
    Thanks for your response. I'm specifically trying to find usage per processor --- which htop does so nicely. Is there a way to access this via `ps`? – DilithiumMatrix Jul 09 '13 at 15:46
  • 6
    thanx, but man make an export to some file to make the thing more usable – themhz May 27 '14 at 13:48
  • 2
    +1 Hi Hisham, htop has no switch -b :) , I'd like to use it this way for output without menu line: htop -b | aha --black – Gerrit Griebel Sep 12 '19 at 12:09
  • htop shows the approximate % of cpu core spending in kernel (e.g. red). that's something ps can't provide. Hence, it would be great htop could be dumped to a file. – HCSF Jan 09 '20 at 01:36
26

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
Marwan Alsabbagh
  • 25,364
  • 9
  • 55
  • 65
  • For an example using ansi2html.sh see https://news.ycombinator.com/item?id=10323333 and linked there is a feature request to add a -b option to htop to make batch processing simpler – pixelbeat Nov 16 '15 at 11:43
  • Why do we need `echo q` at the start? Tried without that and got extra text at the end of the file. UPDATE: To quit `htop` right? – Zain Qazi Mar 10 '20 at 21:40
17

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.

Szymon Jeż
  • 8,273
  • 4
  • 42
  • 60
8

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
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
k0fe
  • 81
  • 1
  • 2
1

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

$htop | recode utf-8 > test.txt

Then cat the file and you should be good.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
Ian
  • 11
  • 1
  • 1
    might be good as a workaround, but when I've tried it, it failed to grab all the process list, and always displayed just a subset, not a reliable way to get the info. – fduff Feb 12 '14 at 09:04
1

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
LoneWolf
  • 11
  • 1
0

Based on the previous answers, I suggest use python to do some post-processing. The codes are as follows:

First, we get the text from htop:

echo q | htop -C > a.txt

Then, we use python to make it human-readable:

import re
htop = open("a.txt").read()
print(re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', "", re.sub(r"\x1b\[\d\d;\dH|\x1b\[\d;3H", "\n", '\n'.join(htop)))[9:])

The results are as follows:

1  [                          0.0%]   Tasks: 11, 38 thr; 1 running
2  [                          0.0%]   Load average: 0.38 0.26 0.11 
3  [                          0.0%]   Uptime: 01:19:50
4  [                          0.0%]
Mem[|#****              700M/25.5G]
Swp[                         0K/0K]
    PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command        
     51 root       20   0  359M 62880 33428 S  0.0  0.2  0:00.00 /tools/node/bin
52 root20   0  359M 62880 33428 S  0.0  0.2  0:00.21 /tools/node/bin
53 root20   0  359M 62880 33428 S  0.0  0.2  0:00.19 /tools/node/bin
54 root20   0  359M 62880 33428 S  0.0  0.2  0:00.16 /tools/node/bin
55 root20   0  359M 62880 33428 S  0.0  0.2  0:00.15 /tools/node/bin
56 root20   0  359M 62880 33428 S  0.0  0.2  0:00.00 /tools/node/bin
57 root20   0  359M 62880 33428 S  0.0  0.2  0:00.05 /tools/node/bin
58 root20   0  359M 62880 33428 S  0.0  0.2  0:00.04 /tools/node/bin
59 root20   0  359M 62880 33428 S  0.0  0.2  0:00.05 /tools/node/bin
60 root20   0  359M 62880 33428 S  0.0  0.2  0:00.04 /tools/node/bin
1 root20   0  359M 62880 33428 S  0.0  0.2  0:08.76 /tools/node/bin
16 root20   0 35892  4768  3660 S  0.0  0.0  0:00.62 tail -n +0 -F /
75 root20   0  190M 61096 13512 S  0.0  0.2  0:00.00 /usr/bin/python
76 root20   0  190M 61096 13512 S  0.0  0.2  0:00.56 /usr/bin/python
F1Help  F2Setup F3SearchF4FilterF5Tree  F6SortByF7Nice -F8Nice +F9Kill  F10Quit
maple
  • 1,828
  • 2
  • 19
  • 28
-1

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.

n1c0
  • 89
  • 1
  • 2