Friday, July 25, 2008

Session Managment with Screen

You log into your remote server via SSH and are busy downloading and installing a new program. In the middle of the download you loose your connection to your server. "Connection Closed" You have just lost your session! Screen to the rescue.

Screen is a window manager for your ssh terminal sessions. Screen is an absolute life saver when working over ssh as it allows you to reconnect to your sessions and continue working exactly were you left off. Screen allows you to re-attach to your session.

Screen is available in your Linux distributions repository. To install screen type

apt-get install screen if you are using a Debian based distro. Or yum install screen if you are using a red hat derivative distribution. You will want to run and install screen onto the machine that you are connecting to. Once installed start screen by typing screen.

If you are presented with a text message just hit enter. If nothing happens don't worry it just means that you are now inside a window within screen and it is running and working.

Screen uses the command "Ctrl-A" to send commands to screen instead of the shell. To get help, just type "Ctrl-A" then "?"

Screen supports multiple windows. This is useful for doing simultaneous tasks on the same machine over ssh without opening new sessions. Sometimes I need to run multiple tasks on the same remote machine or whilst one task is busy running I need to start up another task. Without screen I would need to make a new connection to the same machine or even more multiple connections and if any of the connections drop then i am screwed . With screen you connect to your remote machine only once and can run multiple tasks on the same connection and if your connection breaks, no problem you simply reconnect and re-attach your session.

To open a new window, you just use "Ctrl-A" "c"

run your task, eg mtr www.google.com

Now open a new window with "Ctrl-A" "c" again and start another task this time lets start top type top

To get back to your previous screen (mtr www.google.com task), use "Ctrl-A "n"

You can create multiple windows and toggle through them with "Ctrl-A" "n" for the next screen or "Ctrl-A" "p" for the previous one .

If you want to close your session but want to return to it later then you must detach from your session instead of closing it. This will leave your process running and will allow you to re-attach to the same process later. "Ctrl-A" "d". This will drop you into your shell. All screen windows are still there and you can re-attach to them later.

So you are using screen now and busy downloading a new program and suddenly your connection drops. Don't worry screen will keep the download going. Login to your system and type

screen - R to re-attach to your session and then use Ctrl - A "n" and "p" to toggle between all the sessions you were running on the remote system before your connection was lost.

another useful feature of screen is its ability to monitor a window for activity or for silence.

lets say you are downloading a file and you want to know when the download is finished, you will need to monitor for silence on that screen. To do that type "Ctrl-A" "_" . When your download is complete you will get an alert at the bottom with the window number. To quickly go to that window, use "Ctrl-A" " . After you do this, just type in the number of the window and enter. To stop monitoring, go to that window and undo the monitor with the same command. To monitor for activity type Ctrl-A "M" this will alert you when something new appears on the session that you wanted to monitor.

Screen can also be used to share a terminal session with another user. This is very useful if you need to show someone how to do something.

The host starts screen in a local xterm, using the command screen -S SessionName. The -S switch gives the session a name, which makes multiple screen sessions easier to manage.
Type:
screen -S screendemo
The remote user (bwayne) uses SSH to connect to the host computer (cgerada).
Type:
ssh bwayne@cgerada.computer.ip.address
The host (cgerada) then has to allow multiuser access in the screen session via the command CTRL-A :multiuser on .
Type:
CTRL-A
:multiuser on
Next, the host (cgerada) must grant permission to the remote user (bwayne) to access the screen session using the command CTRL-A :acladd user_name where user_name is the remote user's login ID.
Type:
CTRL-A
:acladd bwayne
The remote user can now connect to the hosts 'screen' session. The syntax to connect to another user's screen session is screen -x host_username/sessionname.
Type:
screen -x cgerada/screendemo

Voila both users will now share the same terminal session.

No comments: