6

OS X has this. Ideally I would like the reboot to appear as if it never happened from the POV of the terminal (let's ignore remote sessions and assume 0 background processes for now). Meaning, I would like to see the stdin, stdout, stderr (i.e. scrollback) history in each of my terminal tabs.

NOTE: This is NOT about bash history. This is about persisting terminal sessions between reboots.

theanine
  • 211
  • 1
  • 7
  • It seems to me that you want the functionality, that is provided by *hibernation* (and it is available with Ubuntu). – sudodus Jun 16 '17 at 21:31
  • 1
    @sudodus No, not exactly. I want my terminal application to be able to manage its own persistence across my machine's state independent of hibernation, just like Chrome does for example. – theanine Jun 16 '17 at 21:55
  • I see. Sorry, but I don't know if there is a terminal window tool, that can do it. – sudodus Jun 16 '17 at 22:06

2 Answers2

2

No, at least in gnome-terminal and other VTE-based emulators it's not possible in an implicit and convenient way.

(You can, of course, explicitly go through all your tabs before the reboot, "Select All" and copy-paste the contents into files, and view those files with some viewer after the reboot. If I understand you, this is not what you're looking for. Also, colors and other formatting would be lost.)

VTE, the terminal emulation widget behind gnome-terminal and many other terminal emulators stores most of the scrollback contents (not the latest bits, though) in temporary files under /tmp by default. The major design decisions that prevent doing what you're looking for are:

  • These files are unlinked right after being created. This is so that they are automatically removed (and the disk space is freed up) even if the terminal emulator exits uncleanly for whatever reason. In order to preserve them, they should be linked back to the filesystem tree (I don't know if it's possible) or copied into another file (which is a slow action if the scrollback is large, let alone properly handling potentially running out of disk space).

  • The files are under /tmp which is wiped out by many distros upon a reboot. A different, persistent location should be chosen instead, or some cooperation is needed with the boot scripts.

  • These files are encrypted (as of VTE version 0.40) to overcome the privacy issue of data leakage in case someone gets access to the disk. The encryption key is only available in gnome-terminal's memory. In order for these files to persist, either the encryption layer should be skipped (bringing back the old privacy concerns), or the keys should be placed on the disk at least temporarily for the duration of the reboot (... er, until that user logs in again and starts up gnome-terminal again... doesn't sound too much better). Feasible only if you don't care about privacy, or if the filesystem is guaranteed to be encrypted.

There are other smaller issues to be addressed as well, e.g. flushing incomplete blocks of these files that contain the last bits of the scrollback data.

I hope I could outline some of the design decisions VTE took that prevent doing what you're looking for. I can't see how your request could be addressed while keeping the current design goals as well. Maybe there's another terminal emulator that does what you're looking for, presumably sacrificing some of VTE's features; I don't know.

egmont
  • 7,790
  • 1
  • 27
  • 37
0

I suggest using the very useful screen tool. It is well worth learning. Repeating in summary an answer I have put on another question:

See in the .screenrc you can initiate default windows with a custom command so you could restore sessions using different .screenrc files if you wish to restore different sessions. screen -t .... See gnu.org Screen Manual - startup files

One good way of having terminal sessions that persist is to run a 'screen' session on server. A 'screen' session gives you multiple windows on that server where you can be logged in to other machines or whatever. You can have multiple 'screen' sessions running on server. You can disconnect and reconnect to the sessions you need. 'screen' is old-school command-line keyboard but well worth learning and playing with to learn how it works. It has a few big advantages:

  • easy to install, runs on any unix(or cygwin), no graphics needed (besides terminal text graphics)
  • gives persistent windowed terminal sessions (as long as server is not rebooted)

* screen quickstart *

RTFM: gnu.org Screen User's Manual

  • screen - Run screen to create a new session simply
  • screen -ls - list screen sessions running
  • screen -r 20229.pts-2.bangor - re-attach to session

The * screen ESCAPE char by default is Ctrl-a * unless some craZy looper has changed it in .screenrc e.g. "escape ^Zz" for people who don't value process backgrounding control. To get a real Ctrl-a to terminal (goto beginning of line) if running in screen then just do Ctrl-a Ctrl-a.

  • ESCAPE c - create new screen window
  • ESCAPE 0 (or 1 2 3 4 5 6 7 8 9) - switch to window 0 (or 1 2 . . )
  • ESCAPE p or ESCAPE n - switch to previous or next window
  • ESCAPE " - window list
  • ESCAPE h - shows screen help

In full here on another question I have put in example of screen help page, command-line usage and example .screenrc:

Terminal emulator with a “restore session” feature like Mac Terminal

gaoithe
  • 521
  • 4
  • 10
  • 1
    While I agree that GNU Screen is worth learning it, its sessions are not persistent over reboots. – Axel Beckert Nov 20 '20 at 04:37
  • @AxelBeckert you run screen on a separate machine. And reconnect to your screen session after reboot/reconnecting. – gaoithe Nov 20 '20 at 09:25
  • 1
    Sure, that's how you (and I) do it. But that's not what the original poster asked for (as I understand it). – Axel Beckert Nov 22 '20 at 01:10
  • But it does answer the question. Often, as in this case, the question is posed by someone looking how to do something without experience and the best answer or tools to use are not quite what is expected. The question is how to get a terminal session to persist. By running screen on a server the terminal sessions persist when you reboot your client. This is a direct answer and what is more it is helpful for people asking this question to know about screen which is common but perhaps a bit obscure if you are not in an environment with experienced users. – gaoithe Nov 22 '20 at 13:26
  • Only _if_ you _have_ a server. I wouldn't expect that most inexperienced users you are targeting your answer at, have access to an SSH server. – Axel Beckert Nov 23 '20 at 17:14
  • No it does not answer the question. Essentially the OP isn't asking to keep session on a system (screen server) while some other (their screen client) system reboots. Nothing against screen, I am a regular tmux/byobu (and ex screen) user. Yet I do seek a solution to persist my terminal contents. This could be presented as a workaround, not an answer; this doesn't solve the issue. – 0xc0de May 01 '23 at 07:01