Using Screen for Session Management in your terminal

Screen is a session management utility for the unix/linux terminal. It serves multiple functions. Say you’re on a linux box and not running X Windows but you have the need to run multiple applications at once, without a window manager like we’re accustomed to in a GUI environment this can be troublesome, enter Screen. Screen allows you to run multiple instances of your terminal and switch back and forth between those instances with a stroke of the keyboard.

But Screen also serves another function. Have you ever been working remotely via an SSH session while in the middle of a running an app or working on a file and all of a sudden the connection simply drops? Again, Screen is here to save the day.

To see if you have Screen installed simply type:

which screen

Installation

In most redhat / centos/ fedora systems this should be in your path already. On my Ubuntu box I had to install it from the repo’s. It came included with my openSuse installation. On the off chance that it’s not preinstalled and not in your repo’s you can download it here: http://www.gnu.org/software/screen/

Using Screen

to call screen, simply type

$ screen

Depending on your install, it may give you a line of text, a page of text, or it may seem nothing happened at all when you ran screen. As long as you didn’t get an error message screen should be running after calling the application. Now you should be sitting in a window within screen. Lets demonstate.

Screen uses the command “Ctrl-A” as a signal to send commands to screen instead of the shell. To get help, just use “Ctrl-A” then “?”. You should now have the screen help page.

How to do multiple windows?

Screen, like many windows managers, can support multiple windows. This is very useful for doing many tasks at the same time without opening new sessions.

To open a new window, you just use “Ctrl-A” “c”. This will create a new window for you with your default prompt. For example, I can be running top and then open a new window to do other things like chat on naim or irssi, or update Twitter with cURL, or edit a file in vi. Go ahead and try it out, since you’ve already started screen, go ahead an “vi file1″ to create a file in vi, then hit “Ctrl-A” and then type “c”. You will be taken to a new prompt. Go ahead and start ‘top’ to view your system processes. Then use “Ctrl-A” and then press “n” to switch back and forth between your open windows.

Leaving Screen

There are two ways to get out of screen. The first is just like logging out of a shell. You kill each window with “Ctrl-A” “K” . I personally use “exit” for each screen I wish to close, when there’s no more screens left, i simply type exit again to 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.

Attach Sessions

So you are using screen and compiling a rather large application. It is taking quite some time and all of sudden your connection drops for whatever reason. But there’s no need to worry, whatever you were running in screen is still running. Re-login to your remote computer or server and use the screen listing tool to see what sessions are running by typing

$ screen -ls
There are screens on:
31619.ttyp2.tuxtraining (Detached)
4731.ttyp2.tuxtraining (Detached)
2 Sockets in /tmp/screens/S-root.

Here you see I have two different screen sessions. To re-attach to a session, use the re-attach command:

$ screen -r 31619.ttyp2.tuxtraining

Just use screen with the -r switch 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’re logged on from one location but will be at another location an hour later, you can still access the screen, same thing if you go to the physical box later on.

Related Posts

Tags: , ,

Leave a Reply

You must be logged in to post a comment.