Try using screen -RR
.
Example:
$ screen -ls
There are screens on:
5958.pts-3.sys01 (08/26/2010 11:40:43 PM) (Detached)
5850.pts-1.sys01 (08/26/2010 11:40:35 PM) (Detached)
2 Sockets in /var/run/screen/S-sdn.
Note that screen 5958 is the youngest. Using screen -RR
connects to screen 5958. The -RR
options is somewhat further explained in the documentation for -d -RR
.
-d -RR Reattach a session and if necessary detach or create it. Use
the first session if more than one session is available.
Another trick I often use is to use -S
to give the screen a tag/label. Then you can reattach using that tag without having to remember what was happening in each screen if the list gets unwieldy.
Example (Launch screens for vim and curl):
$ screen -dm -S curl
$ screen -dm -S vim
$ screen -list
There are screens on:
11292.vim (08/27/2010 12:02:53 AM) (Detached)
11273.curl (08/27/2010 12:01:42 AM) (Detached)
Note: The -dm
option was just used to start a detached screen
And then, at a later date, you can easily reconnect using the tag curl
.
# screen -R curl
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.
Best Answer
You can dump one window of a screen session with
screen -X hardcopy /some/file
, that will save a screen dump of the current window in/some/file
.You can dump a specific window with:
And all of them in a single file with:
You can also dump one file per window, by specifying which directory to dump them in (if you don't specify it, then they'll be dumped in the directory
screen
was started in) with(will be dumped in files called
hardcopy.<n>
)See the
-h
option ofhardcopy
to include the scroll buffer.Use
screen
's-S
option as usual to specify the session to run the command in.