Although I am assuming you know what Screen is and how to use it I will quickly cover the basics just in case. Screen is a wonderful program that creates a “session” in which several “windows” exist. In each window is a shell prompt, from which commands can be executed and programs can be run. The man page for screen can be seen here
It has 2 major uses. The first is that you can open up multiple screens in one ssh session and tab through them all. This decreases the need for multiple ssh sessions. Its especially helpful if you are working with a server that has a limit on the number of ssh connections at any given time. The second and most fantastic reason to use screen is that it allows you to start a session, excecute a script or commands and reattach later to see the results. A example of this would be if you had to excecute a script which may take a few hours, you could start a screen session and the periodically re attach to check on the progress.
So, the example I am going to use today is on a Gentoo linux server. As many people know one of the reasons users do not like gentoo is becasue each package must be compiled from source and given the appropriate USE flag variables. Many times there will be package conflicts and all sorts of other things especially when moving from kde3.5 to kde4 or some other major shift.
Ok lets get started. Gentoo uses the emerge package manager but screen can be useful on any distro which updates. So to start a screen session I type:
- screen -S <name of session>
I will name my session emerge so:
- screen -S emerge
Now this will just take me to a “virtual” terminal but now we can run our command. In this case I am going to emerge openoffice
- r00t@infected ~ $ sudo emerge openoffice
As you can imagine openoffice will take a while to compile so this is a perfect time to detach from our screen with the Ctl-a-d key combination. Screen has lots of options and default key bindings but I am only going to cover the basics. So we go to lunch and we come back and want to see how the compile is going. We ssh back to the box and re-attach to the session by typing:
- screen -rd <name of session>
In case you have forgotten the session name or you have multiple screen sessions going you can type:
- screen -l
This will give you a list of sessions. My sesion was named emerge so heres what I would do:
- screen -rd emerge
And what I would then see is either the compile still going or I would see the prompt if the compile was finished. Neat huh?
So what was this post about? Well think of a situation where you are in a terminal ssh session but you would like more users than your self to see whats going on. This can be useful in many situations. I will stick with my emerge example. Say my compilation of openoffice failed at some point and I needed someone to remote in and give me some help. Naturally I would want to see what they were typing so I could allow more users to attach to my screen session. Its really simple. After you start your screen session just have each of your users ssh in a execute the following command:
- screen -rx emerge
Now I can add as many people as I want to the terminal session and we are all seeing & typing in the same terminal. I have used this in many situations like showing 4-5 people all at once how to compile a C program or something along those lines. The uses for screen are endless and it is by far one of the most valuable shell based tools.
The following table shows the default key bindings for Screen:
Keystrokes | C-a : Command | Action |
---|---|---|
C-a 0 | select 0 | Switch to window 0 |
… | … | … |
C-a 9 | select 9 | Switch to window 9 |
C-a C-a | other | Toggle to the window displayed previously. |
C-a a | meta | Send the command character (C-a) to window. See escape command. |
C-a A | title | Allow the user to enter a name for the current window. |
C-a c | screen | Create a new window with a shell and switch to that window. |
C-a C-c | screen | Create a new window with a shell and switch to that window. |
C-a C | clear | Clear the screen. |
C-a d | detach | Detach screen from this terminal. |
C-a C-d | detach | Detach screen from this terminal. |
C-a D D | pow_detach | Detach and logout. |
C-a k | kill | Destroy current window. |
C-a C-k | kill | Destroy current window. |
C-a space | next | Switch to the next window. |
C-a n | next | Switch to the next window. |
C-a C-n | next | Switch to the next window. |
C-a x | lockscreen | Lock this terminal. |
C-a C-x | lockscreen | Lock this terminal. |
C-a w | windows | Show a list of window. |
C-a C-w | windows | Show a list of window. |
C-a ? | help | Show key bindings. |
Complete Conservative,address function special understanding present depend available among press exercise border name instead none neither over request notice suppose teach according wide may glass arrangement living ready train institute date baby round solution gate story couple fire until contribute ask wrong image past strike space bar throw refuse complete hair fast survey search money black cup concern sign where test reality railway sister experience local world enterprise machine plus add previously success sun policy environment pair line only research effectively outcome attend budget wave physical match cos close