Who says you canít run multiple sessions from one terminal?
Running a graphical environment is great. You can have numerous applications and utilities running, and you can interact with all of them at the same time. Many people who have grown up with a GUI environment look down upon those poor souls who continue to work in a terminal console environment. ďAfter all,Ē they say, ďyou can only do one thing at a time and donít get the same information and control that you have in a desktop environment.Ē
Itís true; they do say those things. (I am curious to know who they are, however.)
Itís also true that the utility of a graphical environment diminishes when you need to administer machines remotely. Do you really want to squander network bandwidth just to maintain a GUI session?
Here are some more questions to ask yourself regarding remote administration:
You are indeed a poor soul if youíve reconciled yourself to the standardsshlogin without any hope of resolving these questions. But all is not lostóthe solution isscreen.
What Is screen?
screenis a terminal screen window manager. That means you can use a console and run multiple terminals at the same time. The fancy term for this ability is multiplexing.
Getting and installingscreenis straightforward using the ports facility:
# cd /usr/ports/misc/screen
Iím working with Version 4.00.01 (FAU) 18-Sep-03.
screenhas many options, settings, and commands. Although weíll attempt to address the major features, the definitive reference is, of course, the manpage.
There are three methods of command-line invocation:
screen [ -options ] [ cmd [ args ] ]
For invokingscreenfor the first time and running specific options and commands
screen -r [[pid.]tty[.host]]
For attaching to and detaching from running sessions
screen -r sessionowner/[[pid.]tty[.host]]
For attaching to and detaching from existing sessions being run by other users
Multitasking with screen
The best way to understandscreenís power is to give you an example of how you can take advantage of it.
Suppose you are sitting at workstationalpha. You want to access your machine,bravo, to download and compile the latest PostgreSQL source code. Log into hostbravoas usercharlieusing SSH:
% ssh -l charlie bravo
Invokescreen. If you give it a session name, with the-sflag, you can address this session by name. This will pay off shortly.
% screen -s A
Go ahead and download the source code now:
% ftp ftp://ftp3.ca.postgresql.org/pub/source/v7.4/postgresql-
Using windows with screen. So far, this has no advantage over a normal SSH login. Yet suppose you need to send some email while youíre downloading. Instead of putting the download into the background, create another screen window with the key sequence C-a c. This symbolizes that the Ctrl key is pressed with the lowercase letter a and then, after releasing them, you press a second key, in this case c.
At this point the console will go blank. Youíll be presented with a second window. Use your favorite email client to compose your message.
Switching between windows.Youíll probably want to switch between the download and mailer windows. Cycle forward in the window list withC-a n. Cycle backward withC-a p, although youíll likely see no difference with two windows.
Splitting windows.Being the efficient person that you are, youíd like to compile the source code as soon as it has downloaded. Even though you havenít completed your email, go back to the original window and extract the tarball:
% tar -xzpvf postgresql-7.4.tar.gz
Wise administrators read the INSTALL file to make sure all the correct options are invoked. Itíd be very handy to be able to read the instructions as you compose the configure command in the same console. screen comes to the rescue here, too: split the window horizontally, running configure in the top half and reading the documentation in the bottom half.
TypeC-a Sto split the screen, where theSis uppercase. You should see a wide horizontal bar in the middle of the screen. The top window will show whatever existed when you split the window. Youíll also see the windowís ID on the left side of the middle bar, along with the name of the shell.
The bottom window doesnít yet have a shell running. Set the focus to the other window withC-a Tab. Create a new window withC-a c, as usual. Notice that the window has the ID of 2 (shown in the bottom lefthand corner); thatís because the email window that you created after starting the download has the ID of 1.
Better window switching.To list all windows associated with this session, use the commandC-a ".
If cycling through windows is onerous, you can also switch between windows by ID. For example,C-a ' 1will go to window 1.
Be prepared for a little confusion because the screen remains split and now shows the window of your choice in the currently focused window. You can quite easily show the same window in both the top and bottom halves.
Enter window 0 withC-a ' 0, and extract the tarball into its own directory. Enter window 2 withC-a ' 2, and navigate to the uppermost directory of the source code to read the INSTALL file.
Naming windows.ID numbers identify windows pretty well, but I prefer to use names of my own choosing. Change a windowís name with the commandC-a A. For example,C-a A email,C-a A source, andC-a A docseem like a big improvement for the currently active windows.
Now, listing the active windows withC-a "will show the following:
At this point, you have one screen session with three windows. Your terminal is split such that it shows two windows at the same time.
Attaching and Deattaching
Suppose you are called away from the workstation in the middle of a sensitive operation (that is, you havenít yet sent your email). TypeC-a x to protect your session. Depending on your configuration, you will either input a password for the screen or use the default account password.
Now suppose you donít return to your workstation. What do you do? You cansshintobravo from a new location and attach to your existing screen session withscreen -dr A. Remember,Awas the name of the screen session.
After finishing and sending your email, kill off that screen. Type the commandC-a kin theemailwindow.
With that business finished, scroll back through the INSTALL text file to find interesting configuration switches. You could retype them, butscreenhas a perfectly capable copy mode. TypeC-a ESC.
Use the cursor keys to navigate to the portions of the document you want to copy. Hit the space bar to mark the beginning of the text to copy. Scroll around to the end of the text with the cursor keys. The current selection will display in reverse video. When youíre satisfied, hit the space bar to copy the current selection into the buffer.
Switch to thesourcewindow and useC-a ]to paste the copied text.
You donít need thedocwindow anymore, so switch into it and eitherexitthe shell or use the key sequenceC-a kto kill it. You could also merge the split screens together with the key sequenceC-a X.
Once youíve started compiling, you can close the terminal but leave the session active by detaching it; just typeC-a d. One of the nice features about detaching the screen is that it is done automatically if you lose connection with the server, so you wonít lose your session. You can reattach to the session later from the same location or from another workstation altogether.
These are only the basics of whatscreencan do. Hereís a quick listing of other features you might enjoy:
You can control all of these features with the .screenrc resource file. Seeman screenfor details.
blog comments powered by Disqus