If you are working with SSH to remote server over an VPN, you may have thought "Can I maintain sessions with SSH even when my connection goes down" as you might have had the bad experience of starting your work all over again... too bad. There is a solution for this... SCREEN, but that’s not all, it has lot of features that'll make your life easy if you are regularly working with SSH.
What is SCREEN
Screen is a full-screen window manager that multiplexes a physical terminal between several processes (In simple terms, it runs multiple terminals at the same time from single console login over ssh session).
The same way tabbed browsing revolutionized the web experience, GNU Screen can do the same for your experience in the command line. Instead of opening up several terminal instances on your desktop, Screen can do it better and simpler. Not only that, with GNU Screen, you can share sessions with others and detach/attach terminal sessions. It is a great tool for people who have to share working environments between work and home.
Feel like geek stuff... lets have a practical session to see how it can help us :)
Login to remote server over SSH
$ ssh me@myserver.com
Start screen session.
You can name your window/session (loadtst1 is name of session):
$ screen -S loadtst1
Let us run "top" command on first session or window (just an example).
$ top
Create new window in same session
Next you would like to do a load test on your new application (just an example, it may be download something from ftp/http site or any thing you wish to do in the remote machine) while you are monitoring the server by "top".
You need to create another screen window by pressing special key combination.
Press CTRL + a followed by c key (first hit CTRL+a, releases both keys and press c).
But how do I switch between these two tasks?
· Switching between windows is the specialty of screen utility. So to switch between "top" and loadtest window (or session) press CTRL+a followed by n key (first hit CTRL+a, releases both keys and press n).
· To list all windows use the command CTRL+a followed by " key (first hit CTRL+a, releases both keys and press " ).
· To switch to window by number use the command CTRL+a followed by window number (first hit CTRL+a, releases both keys and press the number of the window[0-9]).
Leaving Screen
There are two ways to get out of screen. The first is just like logging out of a shell.
You kill the window with "Ctrl-A" "K" or "exit" will work on some systems. This will kill the current windows.
If you have other windows, you will drop into one of those. If this is the last window, then you will exit screen.
The second way to leave screen is to detach from a windows. This method leaves the process running and simple closes the window.
If you have really long processes, you need to close your SSH program, you can detach from the window using "Ctrl-A" "d".
This will drop you into your shell. All screen windows are still there and you can re-attach to them later.
Attaching to Sessions
So you are using screen now and load testing that program. It is taking forever and suddenly your connection drops. Don't worry screen will keep the load testing going. Login to your system and use the screen listing tool to see what sessions are running:
[root@svr1~]# screen -ls
There are screens on:
26335.loadtst1 (Detached)
26403.loadtst2 (Detached)
2 Sockets in /var/run/screen/S-root.
[root@svr1 ~]#
Here you see I have two different screen sessions. To re-attach to a session, use the re-attach command
[root@svr1 ~]# screen -r 26335.loadtst1
Just use screen with the -r flag and the session name. You are now re-attached to the screen. A nice thing about this is you can re-attach from anywhere. If you are at work or a client’s office, you can use screen to start a job and then logout. When you get back to your office or home, you can login and get back to work.
Common screen commands
| screen command | Task |
| Ctrl+a c | Create new window |
| Ctrl+a k | Kill the current window / session |
| Ctrl+a w | List all windows |
| Ctrl+a 0-9 | Go to a window numbered 0 9, use Ctrl+a w to see number |
| Ctrl+a Ctrl+a | Toggle / switch between the current and previous window |
| Ctrl+a S | Split terminal horizontally into regions and press Ctrl+a c to create new window there |
| Ctrl+a :resize | Resize region |
| Ctrl+a :fit | Fit screen size to new terminal size. You can also hit Ctrl+a F for the the same task |
| Ctrl+a :remove | Remove / delete region. You can also hit Ctrl+a X for the same taks |
| Ctrl+a tab | Move to next region |
| Ctrl+a D (Shift-d) | Power detach and logout |
| Ctrl+a d | Detach but keep shell window open |
| Ctrl-a Ctrl-\ | Quit screen |
| Ctrl-a ? | Display help screen i.e. display a list of commands |