31

How do I get top to quit similarly to the way less quits, that is, not leaving behind its output on the screen?

This is the version I have:

procps-ng version 3.3.9
muru
  • 197,895
  • 55
  • 485
  • 740
Arraval
  • 412

5 Answers5

50

When you start it like this ...

top && clear

... the screen clears after exiting top with q.

This acts more like less:

tput smcup ; top ; tput rmcup

(smcup switches to a secondary screen where the top command is executed and when you quit top rmcup switches back to the normal screen)


You could alias the top command to do that ( How do I create a permanent Bash alias? )

Rinzwind
  • 299,756
  • 3
    clear seems to erase all but the last screen of the scrollback buffer too (so just leaving top's last screen in this case) - at least in xfce4-terminal and QTerminal. The tput answer works great, especially in an alias for top – Xen2050 Mar 08 '18 at 10:41
  • 2
    Don't forget Ctl+L for a quick-fix. – jpaugh Mar 08 '18 at 14:41
  • Oh, interesting; I was wondering why less's last output completely disappeared from the buffer. – Lightness Races in Orbit Mar 08 '18 at 15:04
  • Some other suggestions: 1. use the far-superior-but-not-built-in htop, and 2. alias the commands together so you don't have to manually chain them each time. – flith Mar 09 '18 at 06:49
  • @flith I am not going to steal the other users answers ;-) – Rinzwind Mar 09 '18 at 08:04
31

Ironically1, this was briefly the behaviour of top. In 3.3.8, top did clear the screen, but not in versions past or since. See Red Hat bug #977561:

The screen clearing is an indirect result of changes in terminal manipulations which were introduced by Jim Warner (author of the 'top' tool) in order to suppress flooding the 'top' tool with SIGWINCH signals generated by some of the widely used terminal applications. It was me who reported the flooding issue and he tried to do his best in order to resolve it and released a fix for the issue in form of 8 separate commits. The commit introducing the screen clearing has the following hash : 'dedaf6e1a81738ff08ee8e8523871e12f555ad6d'.

Due to the bug report, Jim then fixed it to again not clear the output in 3.3.9.

top has no options or configuration settings to control this, AFAICT, this is entirely hardcoded behaviour. You might want to file a feature request at https://gitlab.com/procps-ng/procps/issues/.

Until then, you could clear manually like Rinzwind suggested, or use tput like steeldriver suggests.


Interestingly, the commit mentioned above also has a nice explanation of the command steeldriver suggested, because it is in fact the same as what was originally done in the code:

A scrollback buffer used in several terminal emulators could be a real inconvenience to a user following some resize operations. Extra keystroke(s) would frequently be required in order to properly render top's display.

After much sleuthing we unearthed two terminfo strings which have the effect of disabling/restoring that darn scrollback buffer. They were well hidden under a title of strings 'to start/end programs using cup'. In turn, 'cup' deals with a tty's cursor addressing capability.

We don't care what you call them or what they refer to so long as they get the job done. And these really do! Be advised, however, that there are some side effects, several of which can even be considered as beneficial:

  • enter_ca_mode/smcup/ti disables scrollback buffering ( and that's good, it's what we had always hoped for )

  • exit_ca_mode/rmcup/te restores the scrollback buffer ( but also restores screen contents existing pre-top ) ( which is different from former program end results ) ( where that last rendered screen was left untouched )

... which corresponds to the tput smcup and tput rmcup commands.


1 Obligatory xkcd.

muru
  • 197,895
  • 55
  • 485
  • 740
15

The behavior you are looking for from less is built in to an alternative called htop. You can install it with:

sudo apt-get install htop

Specifically, it switching to the alternate screen mode before starting preserving your original screen contents when you close htop. It also has a variety of other enhancements over procps's top. Once it's installed, you can just run it as a regular user:

htop
4

You could use watch:

watch top -b

It default to update every two seconds, use e.g. -n1 for one second if you want to change this behaviour. To quit watch, press Ctrl+C instead of the usual Q.

Another approach is opening a new terminal window, e.g. with xterm:

xterm -e top

This behaves exactly like top normally does, the window closes when you quit top with Q and of course it leaves no output in the terminal you started it from. Add & to the end of this command to start it in the background and immediately release the terminal again.

dessert
  • 39,982
1

As Rinzwind's answer points out, the clear command clears top output from the screen.

After exiting top you can also use Ctrl+L to clear the top output from the screen. After either command though, you can still scroll up the screen to see the old top output.

To really clear the screen, such that the top output cannot be scrolled back to, use the reset command:

top && reset

Another option to give that "fresh look" of a brand new session is by running an "ASCII splash screen" (if you have one). In my case I would run:

top && reset && now && screenfetch

reset now splash.png

  • My now script calls weather, calendar (cal) and toilet with fancy time.
  • screenfetch paints the Ubuntu logo and machine stats.
  • 1
    I wouldn't recommend re-running .bashrc regularly without need because usually there are lines like export PATH=...:$PATH in that file and running it multiple times will unnecessarily extend the PATH. Also, calling neofetch in .bashrc is not standard behaviour (although it looks fancy). For instance, when I re-run my .bashrc nothing apparent happens. – PerlDuck Mar 10 '18 at 18:21
  • CTRL+L and clear behave not the same: try for i in {1..200}; do echo "line $i"; done in two different, fresh windows. Then hit CTRL+L in window1 and type clear in window2. Now scroll back in both windows (mouse wheel). In the CTRL+L-window you can see all lines (depending on your scroll buffer) while in the clear-window you can only scroll back one screen. That is: clear clears the scrollback buffer while CTRL+L is similar to $window_height times pressing ENTER (aka newline). – PerlDuck Mar 10 '18 at 18:46
  • 1
    @PerlDuck neofetch is a good catch but it's a derivative I guess called screenfetch. I use it to remind myself I'm in Ubuntu because I have dual boot and Windows Bash has a different splash screen. I'll take out .bashrc reference. As for clear and Ctrl+L my point was they are similar in that they both leave top output in scroll back buffer. – WinEunuuchs2Unix Mar 10 '18 at 23:13
  • 2
    I think everybody has different things in their .bashrc. Try curl wttr.in/Edmonton?0, it's awesome. Use curl wttr.in/:help to get help. I have a similar call in a /etc/update-motd.d/ scriptlet so I get informed when logging in from console or ssh (not for every terminal window). If you like that, add --silent --max-time 3 to the curl call so it won't take forever if internet is slow. – PerlDuck Mar 11 '18 at 11:43
  • @PerlDuck The curl wttr.in/Edmonton?0 is AWESOME! Thank you so much. I'll try to squeeze that in after the toilet time display on my splash screen :) – WinEunuuchs2Unix Mar 11 '18 at 16:13
  • You are welcome. And I knew you would love it. ;-) So did I when I saw it the first time. – PerlDuck Mar 11 '18 at 16:17
  • @PerlDuck After a programming glitch was fixed I managed to get the weather embedded in the splash screen (show above). And you were right I do love it :) – WinEunuuchs2Unix Mar 11 '18 at 21:01
  • Love the MOTD, BTW! =) Just noting here with my system that with the reset and clear commands it all depends on the terminal I happen to be using. In gnome-terminal when I use clear it does clear the last page, and if I run it a couple of more times I end up with a blank scroll back page is all. xfce4-terminal on the other hand keeps all my scroll back buffers and keeps adding in a blank scroll back page. The reset command on the other hand seems universal in the terminals that it clears all the scroll back. – Terrance Mar 13 '18 at 02:26
  • @Terrance On vanilla Ubuntu 16.04 / Unity Ctrl+L clears the screen but you can still scroll back. The clear command also allow scroll back to the top command output but everything before that is erased from the scroll buffer. – WinEunuuchs2Unix Mar 13 '18 at 03:00
  • @PerlDuck I published the weather script but can I ask how you change the city name from Edmonton?: https://askubuntu.com/questions/1020692/how-can-i-get-this-gnome-terminal-splash-screen/1020693#1020693 For example Paris, USA would have dozens of hits. – WinEunuuchs2Unix Mar 30 '18 at 20:14