Guide to a happier unix life - screen

| 8 Comments

Everyone using unix in their job will once in a while have to run a long running process. Usually right before leaving the office to go to dinner. If you have to check on it later to make sure it finished right then you are stuck with redirecting the output to a file and look there later.

Much better solution: Use screen!

It can do lots of things, but the main one in this context is allowing you to "detach" and "reattach" a terminal.

Start by just running screen:

$ screen

Now you should have a new shell. Start some long running command and then press ctrl-a d. (Hold down control and press a, let go of ctrl and press d).

You are now back at the original shell before you started screen. Logout or go home now.

ctrl-a is the "attention" command in screen. If you want to give the program you are running a ctrl-a you can press ctrl-a a. You can remap the attention command to something else if you want.

When you are back at an internet connection you simply run

$ screen -x

to reattach your old session. There are other ways to reattach, but this one is the easiest. If you have multiple screens running it might say something like

$ screen -x
There are several suitable screens on:
        8762.ttyp9.g5  (Attached)
        19775.ttyp9.g5  (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

Then I can either run screen -x 19775 to attach the detached screen or screen -x 8762 to attach the screen that's already attached (another person on another computer maybe - remote pair programming!) (Bonus tip: You just have to type enough of the PID that it's unique, so in this example screen -x 8 or screen -x 1 would be enough).

Easy as pie!

One more trick before you go: In a screen you can press ctrl-a c to create a new "virtual screen". Press ctrl-a n (next) to move to the next virtual screen or ctrl-a p to go to the previous screen.

man screen will give you many more magic keys. screen is such a great tool. Even when I work on small FreeBSD installations on a compact flash card for say Soekris boxes I install screen (but then I also often install perl)...

8 Comments

A friend of mine wrote a nice little introduction to screen(1), which I suggest whenever given half the chance:

A (Brief) Intro to Screen

"If you have to check on it later to make sure it finished right then you are stuck with redirecting the output to a file and look there later."

Or... you could just leave the terminal open!

Or... you could just leave the terminal open!

Not if you are leaving your desk and going home or to the office or some variation of that. Or if the server you are working on has a flaky network connection that might go away.

rjbs: Very nice! Hopefully my 2-minute read will be a nice companion to get people started. :-)

I agree. Screen is great.

I have even found it useful for when I'm staying next to the screen (not just going away to dinner or something). Depending on where I am, sometimes my ssh connection will drop due to bad wireless (or other) connections or due to ssh timeouts while I wait. If I was using screen, I didn't have to dettach the screen...if the connection was lost, the screen is available to reattach when I log back in.

Screen's awesome...it solves the biggest problem I had with Putty, namely that any more than 4 Putty windows and life becomes a hodge-podge of small windows that need to be precisely positioned for optimal use. Doesn't help that Putty can't store positioning info, so I have to spend 5 minutes every time I reboot to arrange my windows pixel-perfectly :)

As for your logging problem, have you tried ctrl+a, ctrl + H? That'll create a log file that all screen output is sent to, typically $HOME/screenlog.${VIRTUAL_SCREEN_INDEX}.

Good tool..
Here is my question,what is the command to attach to a screen created by someone in another computer?

"...Here is my question,what is the command to attach to a screen created by someone in another computer?"

i'm not sure i understand but maybe this will help...you can attach to any screen process whose PID belongs to the user you are currently logged in as. if you need to attach to a screen created by a DIFFERENT user then you need to either login again or su to that user.

if you need to attach to a screen in a different computer altogether just login via ssh or telnet as the user who created the screen in question. does this help?

To attach as someone in another computer, you need to use multiuser screen :
http://aperiodic.net/screen/multiuser

About this Entry

This page contains a single entry by Ask Bjørn Hansen published on February 7, 2006 3:28 PM.

More hardware trouble ... was the previous entry in this blog.

oops, google down is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.38
/* bf */