Screen: All in one

Swobodin's picture
Submitted by Swobodin on Tue, 2006-11-14 13:26. ::
(I learned these tricks via /etc/shadow)
Ever connected to a remote host and needed more than one terminal for a specific task? Every UNIX administrator does, but not all the admins know about screen, as many of them tend to open many login sessions. Aside from the loss of time that causes the previously mentioned operation, every new shell session takes averagely 6 to 8 MB of RAM on the local machine, hence the need for multiple shell windows inside a single session.
Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows.
Too much theory, now, let's proceed on.
Assuming that the remote host you are working on has screen installed (which is almost always the case), connect on your ssh (or telnet) session
ssh swobodin@192.168.0.1
Start a new screen session
screen
Now, you logged in a new screen terminal, which allows you to perform the same operations as in a normal session, yet with other features that can be ran with shortcuts.

Creating, renaming windows, moving between windows

Open a new window into the screen session
Ctrl+a, c (without comma "," Hold Ctrl and A simultaneously, then release and press C in lowercase)
You may rename the window:
Ctrl+a, A (please note, "A" is in uppercase)
Below you will be asked to enter the new title
To switch between windows:
  • Ctrl+a, <window_number>
    where <widow_number> is a digit in the range of 0 to 9 representing the number of the window, 0 stands for the first one.
  • Ctrl+a, space
    Moves to the next window
  • Ctrl+a, "
    Lists the windows and you may select with arrows and Enter keys
  • Ctrl+a, Ctrl+a
    Toggles between the actual window and the previous one
  • Ctrl+a, Ctrl+'
    Type the window title and you move there

Detaching, reattaching windows

To detach a screen and come back to your parent session:
Ctrl+a, d
The windows will work in background, and won't bother you with output and error messages. This is a good idea if you want to disconnect from the remote host and "let the machines do the dirty work".
To reattach a screen (you are in the parent window now), detect its process id, terminal and host
$ screen -d
There are screens on:
        7160.pts-1.host1     (Detached)
        7111.pts-2.host1     (Detached)
        7071.pts-1.host1     (Detached)

Choose the one you want to connect on, and reattach it with -r option
screen -r 7160.pts-1.host1

Locking sessions

To lock a session and requiring a login/password to reconnect (I like this feature), type
Ctrl+a, x
You will have to type your username password in order to return to the session.
Logging output
Ctrl+a, H (uppercase)
All what you type and all what the terminal outputs will be logged in screenlog.n , where n is your window number

Kill, suicide

You may want to kill the screen you are working on
Ctrl+a, K
A prompt menu will you ask you whether you are really willing to kill the window (stupid question, but I don't find an alternative)
To kill the whole screen and the parent session (be careful as you won't have to confirm, all your work will go away)
Ctrl+a, D, D

Miscellaneous features

Ctrl+a, t
Show system information
Ctrl+a, v
Display the version and compilation date.
Ctrl+a, ?
Show bind keys, all what you need is there.

If you like screen

Send bug-reports, fixes, enhancements, t-shirts, money, beer & pizza to the author screen at uni-erlangen dot de
outofctrl
Submitted by outofctrl on Mon, 2006-11-20 10:19.

Hi everybody, I'm back!

Good idea to talk about screen, I'm using it regularly and it really makes the difference. The more important to me with screen is that I do not care if my connection to the server (in case of distant server) shuts down.
When I run a script that will take a couple of hours, it does not matter if I disconnect from the server or not, the script will not be interrupted.

Another tip, for system administrators or everyone how needs multiple connection, so multiple screens, it's hard to find the one you're seeking in such a list :

30270.pts-56.process1 (Attached)
23875.pts-49.process1 (Detached)
4589.pts-25.process1 (Detached)
5519.pts-35.process1 (Detached)

So when you start you screen session you can give it a name like this :

screen -S my_screen_name

And the list will much more readable !

8702.system_update (Detached)
8600.monitoring (Detached)
9629.find_old_config_files (Detached)

You can resume your session later using the -r switch :

screen -r 8702

Or if you know the name, no need to look at the list, do it simpler!

screen -r system_update

Hope it helps :)

-------------------------
OutOfCtrl
FC-6-x86_64

Swobodin
Swobodin's picture
Submitted by Swobodin on Mon, 2006-11-20 10:33.

Yep, naming sessions is better than scripting for the pid; keep in mind that if you forgot to name your session before starting it, you can do it later:
Ctrl+a, A
Regards,

No tengo tierra ni casa
No tengo nombre ni edad
Soy como el viento que pasa
¡Un viento de libertad!