Home arrow Linux arrow General Linux arrow Linux 101 Part 1 - How to use screen
Linux 101 Part 1 - How to use screen Print E-mail
Written by Jason   
Jun 29, 2007 at 07:12 PM

Linux administration can be tough some days, so whenever I find a tool that makes my life easier I hang onto it for dear life. There's one application I use on a daily basis that makes my life much easier, an application that's gotten so intertwined with how I work that I'm not sure I could function without it nearly as efficiently. If you use SSH to manage a server, I urge you to learn to love screen.

More after the jump. 

In a nutshell, screen is a lean, mean window manager for terminal sessions. It's found in every Linux distribution that I've ever used (and that's quite a few), and once you play with it for twenty minutes you'll realize the nirvana that is screen.

1) Fire it up!

screen is found on just about any Linux distribution. It's usually found under /usr/bin/screen. From a command prompt, try this:

which screen

This looks in your path and locates screen. If it returns something, you're good to go!

2) Using screen

screen is pretty easy to use for most things. From a terminal, type:

screen

It should return the following:

Screenshot of screen

Now press Enter. You should be back at a prompt. Now let's make things interesting by running a process:

top

top shows you the top processes running on your machine. Now press CTRL-a then c - this creates a new screen. You should be back at the prompt.

Now type:

screen -ls

This should list your screen session running on your server at the moment:

Result of screen -ls

I'm sure yours looks nicer without the white blobs.

Now let's switch back and check on our top process. You can cycle through all attached (note the (attached) status in the screenshot above) screens with CTRL-a then n. It should return you to the running top process.

Is top still running? Good. But what if you needed to close the window, or even log out of the server? That's where detaching from screen comes in handy. Press CTRL-a and d to detach. This detaches from screen and drops you back to the prompt. Run another screen -ls to see what's happening now:

screen -ls after detaching

Noticed the (detached) status. This means that if you closed the window you're in now, what you had running while in screen wouldn't be closed. It can be very useful if you have a job or script that takes a long time to run.

Let's verify that top is still running. Type:

ps -A | grep top

You should see:

Using ps to see top running

Now let's reattach back to screen. Type:

screen -r <session name>

In the example above, the session name is 10351.pts-0.machinename

You should be back to your session running top. Press q to close top. Now let's setup several screens. Press CTRL-a and c and type pstree. Press CTRL-a and c again and type ls. Press CTRL-a and c again and type top. Now press CTRL-a and c one more time.

On the last one, run screen -ls again. Notice how it only shows one? That's because screen -ls doesn't show individual screens, just screen sessions. That's an important point that many people get wrong.

Disconnect from screen with CTRL-a and d. Now type screen. You should get the screen welcome again. Press Enter, then CTRL-a and d.

Now run screen -ls again. Notice how there's now two screen sessions? Reattach to the bottom one first with screen -r <session name> and press CTRL-a then k to kill a screen:

Terminating screen

Since we only had one screen running in the second session and we closed it, screen terminated that session. Now type screen -ls once again and you should only have one screen session left. Reattach to that one and close each screen with CTRL-a and k.

These are the screen basics. Here's a few more advanced things to try.

Logging in screen can be done by using CTRL-a and H - this creates a log of everything you do (and some of what you see returned) while in the screen. The default filename for the log is screenlog.0 and is created in whatever directory you start screen in. This is useful if you want a record of what you're doing.

If you'd like a menu of your running screens in a session, try CTRL-a and " for a rudimentary menu. You can re-label screens with CTRL-a and A to make the names friendlier.

To rapidly detach a screen and logout all together use CTRL-a and DD. This is a quick shortcut called a power detach that exits out cleanly and quickly from a terminal.

You can lock a screen with CTRL-a and CTRL-x. Very useful if you care about people that might walk up to your desk and start typing things while you're in the bathroom.

You can have screen monitor for activity or lack of activity on a terminal as well, which is useful if you're waiting for something to happen. CTRL-a and M turns on monitoring of a screen to look for movement, while CTRL-a and _ looks for no activity, such as when a script finishes running.

One neat trick is to share a session with someone using screen. You can jump into a screen session by running screen -x <session name>

This is just some of the features of one of my favorite, but often overlooked, Linux applications. Using screen can help make you admin tasks just that much easier, and it's simple to use.

Did I miss anything? Let know! 

Last Updated ( Jun 29, 2007 at 10:09 PM )
� http://www.roboguys.com, Mambo and Designed by Siteground