Save complete GNU screen session state

Is it possible to persist the “state” of a GNU screen window (or just a standard shell) so that I can reload everything after a reboot:

  • The number of opened shells
  • The name of each shell
  • The current directory of each shell
  • The history of each shell
  • If possible, their environment variables

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It is not really possible to save a complete screen session.

What you can do is to create a proper .screenrc which will setup some things after you restarted your system.

Here are some comments to the things you listed:

  • The number of opened shells
  • The name of each shell
  • The current directory of each shell

I use something like this in my .screenrc to open some specific shells on startup:

## set the default shell
shell                   zsh

# screens
screen -t 'zsh'
screen -t 'mutt' mutt
screen -t 'zsh' /home/user/bin/scriptToRun
[..]

You will get the string between ” as your window name and the command after the name will be executed on your default shell. Include any script you want, for example change in a specific directory and open some logs.

  • The history of each shell

Have you ever thought about sharing the history of the shells across your sessions? IMHO this makes things much more easier. In ZSH its done with setopt SHARE_HISTORY in your .zshrc

  • If possible, their environment variables

If you really need this and don’t want any trade-off you could think about a shell script, which reads out the current state of screen, saves the number of shells, environment variables, etc. and puts this information in a startup script called by your .screenrc.
For me this would not be worth the effort because I appreciate a clean environment after a reboot, if I can customize the default windows for screen.

Method 2

I’ve stumbled upon a project called screen-session. It allows you to save and reload GNU Screen sessions to some extent.

Method 3

There is a tool to freeze the state of a process called cryopid. However, currently it doesn’t work with GNU screen as it doesn’t support re-creating a hierarchy of sub-processes. They state that they want to achieve this in the future but the project doesn’t seem to be too active at the moment. Nevertheless, you might want to trace their progress.

Method 4

I built a similar thing on solaris years back. Not even screen-related; it was session related. “if $TTY = whatever”. echox has the right idea.

tip: if you’re doing this across multiple machines, recognize that each machine deserves its own definition of what should be done when you login there, rather than all being the same per your nfs mounted /home or similar.

HTH,
-pbr

Method 5

A script to automate the “saving” of screen sessions can be found here https://github.com/simonwalz/screen-save


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x