Home   Profile   Fun
#68 Linux  03.04.2007

Use screen to return to a session on a remote server even if the connection has been closed


You can easily keep a session on a remote server alive while you disconnect completely from this server. And when you think it's time then you just reconnect and get back exactly to the same session you left before. Also it's possible to work with several virtual terminals at the same time, you can even split them into horizontal regions. But that's not all. There is a way to connect from several different clients to the same screen session. Whatever is done can be seen in all clients simultaneously. There are many more features described in the man page of screen.

Here is how it works:


Overview
Start screen
screen

To log all the console output in the file screenlog.[windownumber] start screen with -L
screen -L

Display all current screen sessions
screen -ls

Reconnect to a screen session, which has the PID 19496
screen -r 19496

Remove dead screen sessions
screen -wipe


Return to a session
Log onto your server and start screen. It doesn't look different but you are now in a virtual terminal managed by screen.
<space>

(Begin your work, e.g. start a very long compilation)

Detach screen from this terminal
<Ctrl-a d>

Now you can safely log off from the server
...
When you like to continue your work, log onto your Server again

Reconnect to the detached screen session
screen -r

As soon as you have finished your work kill the window.
<Ctrl-a k>
or
<Ctrl-a K>


Several virtual terminals
I assume you have already startet screen.

Open a new window
<Ctrl-a c>

Switch to the previous window
<Ctrl-a p>

Switch to the next window
<Ctrl-a n>


Split regions
Split a region into two new ones
<Ctrl-a S>

Set the focus to the next region
<Crtl-a Tab>

If this region is new you have to create a window there, otherwise it's just empty and you have no shell
<Ctrl-a c>

Delete all regions but the current one
<Ctrl-a Q>


Multi display mode
Connect to a not detached screen session. Now two (or more) clients are connected to this session and all can see every action simultaneously.
screen -x