Tuesday, May 24, 2011

How to Use GNU Screen

SkyHi @ Tuesday, May 24, 2011
On UNIX, GNU Screen is a utility that i cannot live without. I know many console users share the same point of view with me.
What is GNU Screen? it is a terminal multiplexer and you can run multiple console-based applications simultaneously. The best part of it is that you can leave it running on remote machines and come back to pick up your console sessions. It’s like VNC or Remote Desktop but for UNIX console. As you know that on UNIX if you run something on a shell and you got disconnected from it then your sessions will also stop. All work will be gone.
With that feature GNU Screen is indispensable when it comes to do console sessions on low speed connection. If you got disconnected then all you have to do is reconnect and continue with your work. Or if you do something that takes a long time to finish, say maybe archiving and backing up many files, then simply launch it from GNU Screen session then detach from it and disconnect. Later on you can come back and reattach again to check the progress.
Anyway, here are some most useful features of GNU Screen that i use most of the time.
GNU Screen Most Used Commands
Commands What It Does
Ctrl-a ? (question mark)
GNU Screen's help pagehelp page
Open the help page. Also remember that “man” and “info” is your friend. By typing “man screen” and “info screen” you’ll get a lot of informations regarding Screen.
Ctrl-a d
resuming GNU Screen's sessionresuming session
Detach from the current Screen session and leave the process running in the background. Use “screen -r” to resume the session. If there are several Screen sessions running then add the session ID to the end of it. Ex: “screen -r 1088“. What really nice is that means you can login from anywhere to start your Screen session then later on continue the session again from different place.
Ctrl-a k
trying to kill a window in gnu screenkill a window
Kill current window. If you have another window active then Screen will move on to that window. If this is the only window to it then Screen will kill the window and exit, therefore ended the session.
Ctrl-a c Create a new window. By default it creates a new shell instance according your system setup (usually 90% of the time this is either bash or tcsh).
Ctrl-a A (capital A)
set title of current window in gnu screenset title
Rename current window. If you have so many windows running then i would suggest you name them so you won’t be lost.
Ctrl-a ” (double quote)
gnu screen showing list of active windows in current sessionlist of active windows
Show list of active windows in the current session.
Ctrl-a n, Ctrl-a p If you have multiple windows running then this command will do the flipping between the next window (using “Ctrl-a n”) and the previous window (using “Ctrl-a p”).
Ctrl-a h
gnu screen telling that it's logging to a file01. logging to a file
log file from a gnu screen window02. the log file
Create a running log (history). It will says “Screen image written to …” (see image 01). The file actually resides in your home directory (see image 02)
Ctrl-a ESC, Ctrl-a ]
entering gnu screen's copy modeA. entering copy mode
highlight the selection before copy it into gnu screen's bufferB. highlight the selection
gnu screen copied text into bufferC. copied into buffer
pasting text from gnu screen's bufferD. pasting text
Press “Ctrl-a ESC” will makes you temporary enter the copy buffer mode (see image A). You can tell when it says “Copy Mode …”.
In copy buffer mode you can move the cursor freely using directional keys. In this example i’m “cat”-ing the content of the Message of the Day (/etc/motd) in my Ubuntu machine. Next step is to highlight the block that you want to copy to the buffer. Move your cursor to the beginning of the block that you want to copy then press enter. That will mark the start point of the block. Then move your cursor toward the end of the block that you want to copy (see image B). In this example i’m copying the text “System Information …” from Ubuntu’s MOTD
Press enter again to copy the highlighted block into the buffer. This will end the “Copy Mode” and put you back to the active screen. You can tell when it says “Copied # characters into buffer” (see image C)
Use “Ctrl-a ]” to paste whatever is in the buffer (see image D). As you can see i’m pasting the text “System Information …” from the Screen’s copy buffer.

REFERENCES

 http://www.phacks.net/how-to-use-gnu-screen/