Using Screen to Manage Multiple Remote and Interrupted SSH Sessions
Attaching to Remote SSH Sessions
In a day of laptops and remote systems, it's often impractical to keep the same ssh session going to a specific server indefinitely. There are times when I want to reconnect from home to an ssh session that I started at work. Unfortunately, ssh doesn't support that sort of thing. But the screen utility lets you do something similar to this.
Screen is great for letting you start a terminal session, walk away from it, and then come back later. Maybe you need to start a long running process such as a complicated data conversion or a multi-hour build. You can use screen to start the ball rolling, go home, and resume the already-in-progress and uninterrupted activity that you started at work.
What screen is and does
According to the first line of the screen man page, "Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)." That's a mouthful. It's also a little misleading.
When I think of a window manager, I think of an application that manages GUI windows on a desktop. When the screen documentation refers to a "window," it means a virtual terminal that is running some application. By "window manager," it means that screen manages one or more virtual terminals. By "full-screen," it means that screen can expand to the limits of the real terminal that contains the screen session. And by "multiplexes," it means that screen can control a number of virtual terminals and switch the view to any one of them and not interrupt the running of any of the others.
While that is the gist of what screen does, that's not the whole picture. When you execute the screen utility, you are really spawning a management process and connected to it. This management process is also known as a screen "session." You can disconnect from the process at any time and leave everything running just as if it were running in a terminal. You can also create a number of different virtual terminals, each running its own application, and switch among them. When you create these virtual terminals, you can almost think of them as part of a tabbed terminal emulator such as Terminal.app on Mac, gnome-terminal on Gnome, or konsole on KDE, except without the visible tabs.
Creating screen sessions
You create a screen session by running the "screen" command line utility. If you run "screen" with no command line arguments, screen will create a new session for you and drop you into a shell. The first thing you see will be a banner that looks like this:
Screen version 4.00.03jw4 (FAU) 2-May-06Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder Copyright (c) 1987 Oliver LaumannThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to email@example.com 'Press Space or Return to end.'
After you hit space or return (or enter), you will be dropped into a shell. At this point, you can list all the screen sessions that you are running. If you run "screen" with a "-ls" argument, screen will display a list of your screen sessions:
jmjones@dinktrepid:~$ screen -ls There is a screen on: 7596.pts-1.dinktrepid (01/30/2009 07:45:37 AM) (Attached) 1 Socket in /var/run/screen/S-jmjones.
The state of this screen session is "Attached." When you start a new screen session with no arguments, you will automatically be "attached" to it. The number "7596" indicates the process ID of the screen management process. I'll discuss later how this information is useful. (Hint, I said something about detaching and re-attaching to screen sessions earlier.)
Sponsored by BlackBerry
BlackBerry® Enterprise Server Express enables businesses of any size to quickly and easily get started with the BlackBerry solution. It provides advanced BlackBerry smartphone features with no additional software or user license fees, and works with any Internet-enabled BlackBerry data plan or a BlackBerry enterprise data plan. Download now!