Search code examples
linuxcachingsshterminalkonsole

How does console/shell caching/restoration work?


This question is purely out of curiosity; there's no problem to be resolved.

Note: I'm using Konsole on CentOS 7.

When I SSH to a remote box and leave the session inactive for a while, it times out. As a workaround, I use less [some-file] to keep the session active.

Obviously the sessions still break when I suspend the VM at the end of the day.

The next day when I start a new SSH session, everything is normal except that some Konsole functionality doesn't work (namely, clearing scrollback, which I use a lot) for the same terminal window used for the SSH session.

However, after I less a file and quit, two things happen:

  1. Konsole resumes working normally.
  2. Whatever output was in the terminal before I used less is replaced with whatever output was there the previous day, before the session was broken by shutting down the VM I'm SSHing from.

Curiously, this happens for all ~4 concurrent terminals/sessions (each one having its output from the previous day restored). [edit: clarification: each of the ~4 terminals exhibits this behavior when I SSH/less in that particular terminal. Each one resumes its output from the day before. Doing this in one terminal/session is not affecting the other terminals/sessions]

I assume this would happen with any terminal app (e.g. vi, nano, etc.) with its own "gui" and isn't specific to less.

Seems like there's some caching going on. I assume quitting less (or vi, nano, etc.) triggers some sort of shell output restoration in general, but this particular manifestation seems somewhat odd.

Any idea what's going on?


Solution

  • The terminal feature described in the question the alternate screen feature (originally xterm, but copied/imitated by several other terminals including konsole). Depending on the terminal description, you may/may not use this feature.

    less and most full-screen terminal programs such as vi send the escape sequences to switch to/from alternate screen if they're defined in the terminal description (i.e., TERM=xterm).

    From the description, it sounds as if you're using different tabs in the same instance of konsole, and that it's remembering that your terminal was set to the alternate screen. konsole and some other programs attempt to save/restore "session" information when halted, so that probably seemed like a good thing to save/restore.

    While in the alternate screen, terminal programs typically have little or no access to the scrollback region.

    You can use tput to send the same escape sequence (without running less):

    tput rmcup
    

    (If the terminal description doesn't have a definition for that, it'll do nothing).

    Further reading: