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
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
When you start it like this ...
top && clear
... the screen clears after exiting top with q.
This acts more like
tput smcup ; top ; tput rmcup
smcup switches to a secondary screen where the
top command is executed and when you quit
rmcup switches back to the normal screen)
You could alias the top command to do that ( How do I create a permanent Bash alias? )
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.
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:
You could use
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 -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.
As Rinzwind's answer points out, the
clear command clears
top output from the screen.
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
To really clear the screen, such that the
top output cannot be scrolled back to, use the
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
nowscript calls weather, calendar (
toiletwith fancy time.
screenfetchpaints the Ubuntu logo and machine stats.