Wednesday 16 April 2008

screen : Manage multiple sessions over a SSH connection

Screen is a popular utility that every system admin must know about. It's essentially a window manager for your shell sessions so that you can work on multiple shells at once without opening multiple connections to the server, Screen is specially useful if your connection is unreliable and has a tendency to drop while in the middle of something. Screen keeps running in the background even if you get disconnected, so you can imagine what a life saver it is.

I summarize some of the most important commands here for my future convenience. But the best way to get information about screen is to read the man pages.

After opening a connection to the server, simply run 'screen' to start screen. It might appear that nothing happened, but be assured, you're now in one of the screen 'windows'. To check, simply run:
screen -ls
The output will be similar to the following:
There is a screen on:
23876.pts-1.TuxMaster-JanuZ (Attached)
1 Socket in /var/run/screen/S-januz.

Once screen is running, there are several keyboard shortcuts that can be used. Some of the most important ones are:
C-a-c : Open another 'window'
C-a-n : Switch to next 'window'
C-a-p : Switch to previous 'window'
C-a-d : Detach the 'window'. The commands running in the window keeps running in the background even if you get disconnected.
C-a-h : Create a log of the session
C-a-? : Display a help window with all the keyboard shortcuts

If you got disconnected from a session and want to resume from where you left off, simply log back in and run the following set of commands to re-attach to the windows:
First list the running screen sessions
screen -ls
The output will be similar to
There is a screen on:
23876.pts-1.TuxMaster-JanuZ (Detached)
1 Socket in /var/run/screen/S-januz.
To re-attach, run screen with -r option, giving the session name (23876.pts-1.TuxMaster-JanuZ).
screen -r 23876.pts-1.TuxMaster-JanuZ
Sometimes, simply typing
screen -r 23876
also works.

Check out the man pages for more information or Google around for a tutorial. Mastering screen will be one of the best moves you will ever make.

No comments: