Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Power backgrounding with 'screen' UNIX
One of my absolute favourite *nix-tools is screen. It allows me to start a lengthy and possibly talkative and interactive process and then detach from it at will. I can later attach to the process and then enter right into where it is at the moment.

You can install screen using fink (which has been discussed previously on macosxhints.com), or you can download the source and build it yourself from the screen home page on gnu.org.

Read the rest of the article for a theoretical example of how to use screen once it's installed.

The following is a fictive but plausible screen session. The gnome bundle is large and complex 'fink' package that takes quite a while to install; a perfect situation in which to use screen. So prior to starting the fink install, start a screen session:
 % screen
Then launch the fink installer for the gnome package:
 % fink install bundle-gnome
fink might ask me a couple of questions before it starts downloading. So I answer them and then when it starts downloading, I hit "control A" and then "d" (default detach key sequence). I can now log out. I can log in again hours later to see where the install process is at the moment:
 % screen -R
fink can now be downloading or compiling or whatever. It could also be asking me a question. Like if it has had problems getting a file and is asking if I wish to "Give up" or "Retry". I want fink to retry and tell it so. I can now detach (ctrl-a d) again. Hours later I can check in on the install process again using "screen -R".

This time maybe fink has told me the install was successful and I then see the shell prompt the screen started for me to begin with. I can then exit that shell:
  % exit
I get a message [screen exiting] (or something like that), and get back the prompt from where I wrote "screen -R".

I often start a screen right after I have logged in. Then I can detach at any time, even if I'm in the middle of editing a file in vim or some such and then reattach and continue editing later.

screen is almost to good to be true. =)
    •    
  • Currently 3.80 / 5
  You rated: 5 / 5 (5 votes cast)
 
[7,624 views]  

Power backgrounding with 'screen' | 13 comments | Create New Account
Click here to return to the 'Power backgrounding with 'screen'' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Precompiled screen
Authored by: gevans on Mar 16, '02 12:43:36AM

There is a precompiled screen .pkg at http://macosx.forked.net under miscellaneous in case anyone doesn't want to compile it.



[ Reply to This | # ]
Precompiled screen
Authored by: bhines on Mar 16, '02 06:08:16AM

Screen is in fink stable, so you can use apt-get and you won't have to compile it.

Just type "sudo apt-get install screen".



[ Reply to This | # ]
apt-get
Authored by: osxpez on Mar 16, '02 08:23:09AM

Wow, can you even imagine how much time that little piece of knowledge would have saved me?! You should post a hint about apt-get.



[ Reply to This | # ]
three cheers for screen
Authored by: robh on Mar 16, '02 06:35:40AM

I've been using screen for as long as I can remember.

My favourite use for it was to connect to ten machines I used to administer remotely. I'd have a window inside screen for each machine and would hop from one to another.

My other great use for screen was to connect to a remote machine, run screen on it and then start some long running process. I could then detach screen and logout from the machine and return to it hours or days later and reconnect to the screen session and continue from where I finished last time.

Running screen on a remote machine also protects you from sudden unexpected network outages because you can reconnect to the remote machine and then resume the interrupted session as if nothing happened.



[ Reply to This | # ]
three cheers for screen
Authored by: david-bo on Nov 16, '02 09:04:43PM

What is the difference between 'detach' a screen and simply close the terminal? And imagin that you have two terminal windows open simultaneously, if you issue screen onboth of them and then closes them, and later open two new terminals, how do you pick which screen to reattach to with screen -R?

The screen man-page is not that clear...

Thanks



[ Reply to This | # ]
new terminal window?
Authored by: kerim on Mar 16, '02 07:42:53AM

Excuse my igrnorance, but how is this any different from simply opening a new terminal window?



[ Reply to This | # ]
new terminal window?
Authored by: osxpez on Mar 16, '02 08:09:36AM
It differs in that you can't close the terminal window and then later expect to be able to attacch to the process that was running when you closed it. This you can do with screen. I often start lengthy, interactive, processes from screen on my computer at home. Then when at work I can check in on the process by logging in to my home computer and attaching to the screen started process. Or vice versa when I start a download or a compile or a sequence of tests or whatever on one of the machines at work. I can then attach to the processes from my home computer.

But you got it right in that screen is very much like an ordinary terminal window. It's just a tad more virtual. =)

[ Reply to This | # ]
new terminal window?
Authored by: Anonymous on Mar 17, '02 10:27:57AM

The advantage of using screen over simply opening a new terminal window is that you retain all the input/output interactivity that may have taken place. If you start a long running task in the background, log out, and then log back in after some time (say a few hours) with a new terminal window, you would've lost any output the long-running task might have generated while you were logged out.



[ Reply to This | # ]
Too good to be true
Authored by: k2r on Mar 16, '02 12:38:39PM

Thank you for this posting.
I've been into *nixes for 9 years now
and I didn't know this great tool.

k2r



[ Reply to This | # ]
why i love screen
Authored by: mkoz on Mar 16, '02 01:41:01PM

i love screen, when ever i install or start working on a new box i install screen first. i usually run with 3 to 5 screens at a time.

for example one of the machines i do web development on i usually have one screen running just pine (% screen -t pine 2 pine -i), then there is one for mysql (% screen -t mysql 3 mysql) there is one watching log files (% screen -t log 0 tail -f /var/log/messages), the 3 or more to edit files and move around the file system.

on my os x machine i usually have one terminal window running 5 screens for whatever, so i can be in the middle of something and switch screens and do something else, then go back to what ever i was doing all without cluttering my desktop with terminal windows.

if you want to watch more than one screen at a time just split the window (ctl-a S) and switch to the screens you want to look at, read the man page for the details.

another example, you can have a couple of c files open in vi each in their own screen, then you can compile in another screen, if an error occurs you can switch to a screen where your file is still open at the same place you left it, switch back to the screen where you were compiling because you forgot which line number the error was on, then switch back fix the error, switch back and try compiling again.

so, read the man page for screen, learn all you can. another great command for working on remote machines is '% screen -D -R' read about it. enjoy.



[ Reply to This | # ]
Source doesn't auto-configure correctly (and fix)
Authored by: Cadre on Mar 16, '02 05:11:10PM

Out of the box, the screen source won't compile correctly. It doesn't appear to be auto-configuring correctly.

After you have run configure goto line 55 of screen-3.9.11/os.h change the <unistd.h> to read <sys/unistd.h> and then run make and it will compile without errors.



[ Reply to This | # ]
Source doesn't auto-configure correctly (and fix)
Authored by: rocteur on Jan 04, '03 06:35:45AM

Thanks very much for this tip.

I was trying to compile 3.9.13 and was getting errors, did a search on Google and found your answer about unitstd.h.. And it compiled fine after that!

Thanks very much!!!



[ Reply to This | # ]
screen for collaboration
Authored by: sabi on Mar 17, '02 01:02:07PM

You can also use screen as a kind of console Timbuktu or VNC, with the screen -x (multi attach) method. Multiple people can attach to the same screen and collaborate on their work.

The equivalent of screen for Emacs is gnuclient/emacsclient. While more special-purpose, they're even better because you can switch between Aqua/X emacs and console emacs, so you can start editing a file from the GUI in one place and pick it up from a terminal session somewhere else.

I wrote a small emacsclient-launching script which is compatible with the Carbon port of GNU Emacs... it's here:

http://web.sabi.net/log/2002/03/05.html#a227



[ Reply to This | # ]