How to save htop output to file?
Solution 1
Ron provided a link to htop output to human readable file and a quote from the top-voted answer by the developer of htop.
However, a little further down on the same page, there's a solution which uses ANSI to HTML conversion. Briefly, install aha
from the software center and then run:
echo q | htop | aha --black --line-fix > htop.html
Here's what the first few lines look like:
1 [||||||||||||||||||||||||||||||||| 42.9%] Tasks: 73, 251 thr; 1 running
2 [|||||| 7.1%] Load average: 0.28 0.32 0.32
Mem[|||||||||||||||||||||||||||||||||| 581/3916MB] Uptime: 03:09:25
Swp[ 0/4056MB]
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command
9785 dkb 20 0 31544 2464 1312 R 22.2 0.1 0:00.09 htop
3503 dkb 20 0 1065M 59684 16344 S 7.4 1.5 6:25.43 mpv --profile=pseudo-gui -- file:///home/dkb/Downloads/ONX
1 root 20 0 33760 3088 1488 S 0.0 0.1 0:01.62 /sbin/init
276 root 20 0 19472 652 460 S 0.0 0.0 0:00.19 upstart-udev-bridge --daemon
281 root 20 0 52592 2640 1016 S 0.0 0.1 0:00.76 /lib/systemd/systemd-udevd --daemon
577 root 20 0 15256 632 388 S 0.0 0.0 0:00.05 upstart-socket-bridge --daemon
662 root 20 0 15272 416 200 S 0.0 0.0 0:00.04 upstart-file-bridge --daemon
677 syslog 20 0 249M 1304 836 S 0.0 0.0 0:00.01 rsyslogd
678 syslog 20 0 249M 1304 836 S 0.0 0.0 0:00.00 rsyslogd
679 syslog 20 0 249M 1304 836 S 0.0 0.0 0:00.02 rsyslogd
675 syslog 20 0 249M 1304 836 S 0.0 0.0 0:00.04 rsyslogd
680 messagebu 20 0 39860 1968 1044 S 0.0 0.0 0:00.52 dbus-daemon --system --fork
Solution 2
Right from the author of htop
:
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).
Solution 3
Try this command; it does what you want. You just need to install aha
and html2text
first.
echo q | htop -C | aha --line-fix | html2text -width 999 | grep -v "F1Help" | grep -v "xml version=" > file.txt
Solution 4
this can get colorful output same as you see when htop running.
1. save terminal output to file
echo | htop > /tmp/htop.out
## OR
sleep 3 | htop > /tmp/htop.out
//htop.out has terminal save/load console_sequence
2. show output in terminal
head -c -10 /tmp/htop.out | tail -c +10
// just use head & tail strip the screen reset console_sequence
// 10
bytes was roughly
// only show last screen in htop.out
X. explanation
vim /tmp/htop.out
,
will see something like this ( @ ubuntu18, gnome-terminal, TERM=xterm )
^[[?1049h^[[22; .... ^[[?1049l^[[23;0;0t^M^[[?1l^[>
-
^[[?1049h
: switch to alternative screen @ begin of htop -
^[[?1049l
: switch to normal screen @ end of htop
htop.out is not blank, it just show content in alternative screen, no effecit with normal screen.
the head
tail
strip out those special sequence, let it show content at normal screen.
@brad
// seems the watch
command also use this mechanism
--- refs: ---
https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-The-Alternate-Screen-Buffer
https://unix.stackexchange.com/questions/288962/what-does-1049h-and-1h-ansi-escape-sequences-do
https://stackoverflow.com/questions/11023929/using-the-alternate-screen-in-a-bash-script
Solution 5
I normally do the following:
top
control-c
and then scroll up so I can copy/paste:
rinzwind@schijfwereld:~$ top
top - 18:58:05 up 13 min, 2 users, load average: 0,18, 0,35, 0,24
Tasks: 252 total, 1 running, 251 sleeping, 0 stopped, 0 zombie
%Cpu(s): 3,4 us, 0,7 sy, 0,5 ni, 95,3 id, 0,1 wa, 0,0 hi, 0,0 si, 0,0 st
KiB Mem: 12190608 total, 2007692 used, 10182916 free, 50292 buffers
KiB Swap: 24414204 total, 0 used, 24414204 free. 739236 cached Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1093 rinzwind 20 0 364616 8824 7252 S 6,2 0,1 0:00.94 ibus-daemon
2525 rinzwind 20 0 29192 3048 2540 R 6,2 0,0 0:00.02 top
1 root 20 0 185008 5564 3840 S 0,0 0,0 0:00.77 systemd
2 root 20 0 0 0 0 S 0,0 0,0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0,0 0,0 0:00.00 ksoftirqd/0
5 root 0 -20 0 0 0 S 0,0 0,0 0:00.00 kworker/0:+
7 root 20 0 0 0 0 S 0,0 0,0 0:00.32 rcu_sched
8 root 20 0 0 0 0 S 0,0 0,0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0,0 0,0 0:00.09 rcuos/0
10 root 20 0 0 0 0 S 0,0 0,0 0:00.00 rcuob/0
11 root rt 0 0 0 0 S 0,0 0,0 0:00.00 migration/0
12 root rt 0 0 0 0 S 0,0 0,0 0:00.00 watchdog/0
13 root rt 0 0 0 0 S 0,0 0,0 0:00.00 watchdog/1
14 root rt 0 0 0 0 S 0,0 0,0 0:00.00 migration/1
15 root 20 0 0 0 0 S 0,0 0,0 0:00.00 ksoftirqd/1
16 root 20 0 0 0 0 S 0,0 0,0 0:00.04 kworker/1:0
17 root 0 -20 0 0 0 S 0,0 0,0 0:00.00 kworker/1:+
rinzwind@schijfwereld:~$
Related videos on Youtube
KingWolin
Updated on September 18, 2022Comments
-
KingWolin over 1 year
I'm trying to write a query that will calculate remaining total points for each day when some points have been completed.
Story points completed in a day
So if my total is 100 points, it should show 99, 97.5, 94.5 as I want to use it in a burndown graph in Power BI.
If I use LAG, it will calculate the remaining for each row separately, instead of using 'updated' total from previous row.
Also - for rest of the days with no change in points it should show most recent remaining total.
Any ideas?
-
Zeph almost 2 yearsWhat have you tried? See codingsight.com/…
-
-
BallpointBen almost 7 yearsFor those wondering: piping
q
tohtop
quits it immediately. -
brad almost 4 yearscan you explain this in a little more detail? "just use head & tail strip the screen reset console_sequence"
-
yurenchen over 3 years@brad strip out some special char sequence, ( such as
echo -ne "\e[10;50H \e[1J"
, will move cursor and erase display, ( more detail found atman console_sequence
-
asgs over 3 yearsHahahahahahahah
-
Tik0 over 3 yearsI like to add
stty
to expand the rows such that every process is in the html file:stty rows 9999; echo q | htop | aha --black --line-fix > htop.html