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

The power of 'screen' revisited UNIX
A recent freshmeat article introduced me to the wonderful world of GNU screen, a console-management program. It's built in to Mac OS X, and is a great benefit to users of the Terminal.

Type 'screen' in your Terminal to try it out now; read on for hints on using and configuring it.

[Editor's note: Although we ran a great hint on screen a while back, now that it's included in OS X (as of 10.2, I think), I felt it worth another mention, and this hint has some additional tricks up its sleeve for screen.]

Screen has many features; I mostly use two:

1. Detach from a session, and reattach later
Suppose you're in a console, with Terminal.app or remotely via SSH. You're doing something long and involved, like compiling a Fink project, or editing a config file. Suddenly you need to close your SSH connection, or log out to let someone else use the computer. Will you have to stop what you're doing and lose your place? Not with screen. Just close the Terminal window (or type Ctrl-A, Ctrl-D) to Detach from the session, leaving your work running in the background. Later, type screen -r to reattach to your session, and pick up right where you left off!

2. Multiple 'virtual' consoles in one terminal window
Typing Ctrl-A, Ctrl-C while in screen will Create a new console session, while leaving your existing one intact. Then press Ctrl-A, Ctrl-A to flip back and forth. Although you can do this by just making a new window in Terminal.app, I find it useful when I'm connected via SSH to my machine, since it can be annoying to open a whole new SSH connection (e.g. when connecting from a web browser with the Java SSH terminal).

Some general screen hints and helpers

Using screen as your shell
Being able to Detach from work in progress is wonderful, but doesn't do much good if you have to remember to explicitly start screen before you start working. So I've configured my shell to start screen automatically when I log in, and to remind me if I have any detached sessions waiting.

1. Customize screen
By default, screen detaches and saves your session anytime you quit Terminal without typing 'exit'. I do this all the time, so I found it annoying to have all the old sessions piling up. To fix this, create a file named .screenrc in your home directory, with the following contents:
 autodetach off
startup_message off
The second line turns off the annoying welcome message that appears when you start screen.

2a. If you use tcsh, to start screen automatically, set your .login file to read simply:
 exec screen
To receive a helpful message reminding you of any detached sessions, add the following to your .cshrc file:
# ---------------- screen setup ----------
alias reattach "screen -r"
if ( "$TERM" == "screen" ) then
if (!~ $?SHOWED_SCREEN_MESSAGE ) then
set detached_screens=`screen -list | grep Detached`
if ( "$detached_screens" != "" ) then
echo "+-------------------------------------+"
echo "| Detached screens are available: |"
echo "$detached_screens"
echo "+-------------------------------------+"
else
echo "[ screen is activated ]"
endif
setenv SHOWED_SCREEN_MESSAGE true
endif
endif
# ----------------------------------------
This also adds an alias, so you can type reattach to reattach to one of your detached sessions.

2b. If you use bash...
Many folks, including me, prefer to use bash as their login shell. If you do, to start screen automatically, set your .profile file to read:
 exec screen
Just as with .login for tcsh users.

Likewise, add the following to your .bashrc file:
# Login greeting ------------------
if [ "$TERM" = "screen" -a ! "$SHOWED_SCREEN_MESSAGE" = "true" ]; then
detached_screens=`screen -list | grep Detached`
if [ ! -z "$detached_screens" ]; then
echo "+---------------------------------------+"
echo "| Detached screens are available: |"
echo "$detached_screens"
echo "+---------------------------------------+"
else
echo "[ screen is activated ]"
fi
export SHOWED_SCREEN_MESSAGE="true"
fi
As always, man screen will reveal many, many more details about using screen.
    •    
  • Currently 3.33 / 5
  You rated: 3 / 5 (6 votes cast)
 
[47,711 views]  

The power of 'screen' revisited | 20 comments | Create New Account
Click here to return to the 'The power of 'screen' revisited' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
using another command key
Authored by: Jay D on Nov 14, '02 11:19:45AM
since Ctrl-A binds to other useful stuff, I use Ctrl-Z for invoking screen commands. start screen like this:
screen -e '^Zz'
To send a 'real' Ctrl-z, i.e. 'unix suspend,' type Ctrl-z then z.

[ Reply to This | # ]
a few observations...
Authored by: thatch on Nov 14, '02 01:25:33PM

Once screen was invoked, I lost the correct colors in my fink installed ls command with dircolors. Some of the colors were changed to diffferent colors and others just plain disappeared.

I also lost the ability to do case insensitive tab completion. When I terminated screen, all was back to normal except I had no save history of the terminal commands during the screen session.

There could be more conflicts. I didn't spend a lot of time on it yet. Those mentioned above were just immediately noticeable.



[ Reply to This | # ]
a few observations...
Authored by: mithras on Nov 14, '02 05:21:33PM

if those settings are configured in your .login or .profile, they won't carry over to the subshell launched by screen. make sure they're set in .tcshrc or .bashrc.

the color settings could be affected by the $TERM environment variable. you could try manually setting it in your .tcshrc or .bashrc and see if that makes a difference.



[ Reply to This | # ]
a few observations...
Authored by: thatch on Nov 14, '02 08:59:08PM

mithras,

My settings are already in ~/.tcshrc. Color, as well as case insensitive tab completion and save merge history, has worked fine without screen invoked. So, I've never had a reason to change the $TERM env variable.

It occurred to me that maybe the system installed version of screen is different from the latest build ported through fink. Upon checking, I found that the system version in 10.2.1 is 3.09.10 (FAU) 4-Sep-01 and the fink version is 3.9.11-1. I will upgrade and see if it makes any difference.

I'll carry this onto the forums from here on out should I need to continue. Thanks for your reply.



[ Reply to This | # ]
a few observations...
Authored by: bluehz on Nov 15, '02 11:17:48PM

I also have two version of screen - the newer being in my fink install. Other than the obvious (calling by path) is there an easy way to get cmd to default to teh fink install in the case of identical commands like this. For example - unlaltered - if I type screen - I get the /usr/bin/screen as opposed to the fink.



[ Reply to This | # ]
a few observations...
Authored by: thatch on Nov 16, '02 03:14:15AM

bluehz, your $PATH must have /sw before /usr/bin and then you should see:

% which screen
/sw/bin/screen



[ Reply to This | # ]
Kills XDarwin
Authored by: bluehz on Nov 17, '02 07:41:20AM

I noticed after initiating this hint - I was no longer able to launch XDarwin. I don't normally use .cshrc and .tcshrc (instead keeping old style ~/Library/init/tcsh file), but I created .cshrc and .tcshrc file just for this hint. I could then no longer launch XDarwin, Delete teh two "dot" files and XDarwin launches fine again.



[ Reply to This | # ]
Re: Kills XDarwin
Authored by: thatch on Nov 17, '02 06:07:56PM

bluehz,

Using the old style /Library/init/tcsh/*.mine file in Jaguar can cause problems. I'd recommend using only the ~/.tcshrc and ~/.login files to tweak your environment stuff, except for aliases.mine which still seems to work fine for me. See the hint on this site for re-establishing your .mine file functionality if you are on Jaguar.



[ Reply to This | # ]
Re: Kills XDarwin
Authored by: bluehz on Nov 18, '02 08:38:14AM

I have sorta been meaning to switch to the new style - but every time I make attempts at switching - I get more problems than good. I guess the old style is just comfortable...hahahah (weird to say about pure unix guts...ehhh). I'll take a look and see if I can find the articles talking about this on OS X Hints and see if I can give it another shot. Not sure how my enabling the .tcshrc will help with this hint since it clearly caused a problem with my XDarwin once enabled.



[ Reply to This | # ]
Re: Kills XDarwin
Authored by: bluehz on Nov 19, '02 11:33:24AM

I have read the docs on old-style tcsh dir and fail to see any problems generated from this method. Can you point me to some specific docs?



[ Reply to This | # ]
a few observations...
Authored by: mithras on Nov 18, '02 11:19:48AM

Sorry I didn't make this clear before.

Screen *changes* the $TERM environment variable when it starts up. That variable is commonly checked by terminal programs when they are deciding how to display their output. So programs that worked fine when $TERM was not set, or was set to 'Apple Terminal' or somesuch, might stop working under screen.

So adding a line in your .tcshrc to change $TERM back to blank or whatever it was before might fix things.



[ Reply to This | # ]
hows about
Authored by: geohar on Nov 14, '02 05:55:54PM
hows about...
if (!~ $?SHOWED_SCREEN_MESSAGE ) then
        set detached_screens=`screen -list | sed -n 's/^(.*)(Detached)$/|1/gp'`
        if ( "$detached_screens" != "" ) then   
                echo    "+-----------------------------------------------+"
                echo    "| Detached screens are available:              |"
                echo -n "$detached_screens"
                echo    "               |"
                echo    "+-----------------------------------------------+"
        endif
        setenv SHOWED_SCREEN_MESSAGE true
endif
in your rc.mine script. Then you dont have to run screen as your shell, and (I'm a sucker for formatting - and unix arcane commands as you can see), the output will have nice | and | either side. You might have to tweak the tabs depending on your shell settings and what geeklog does to my submission:)

[ Reply to This | # ]
Troubleshooting screen
Authored by: vonleigh on Nov 14, '02 08:14:55PM

Hello,

I'm currently trying to troubleshoot this in the forums. Basically what happens is that when I invoke screen (or sudo -s for that matter) It drops me back into a tcsh shell instead of my current shell (using zsh).

Any tips are appreciated:

<http://forums.macosxhints.com/showthread.php?s=&postid=34526>


v



[ Reply to This | # ]
Other screen tips
Authored by: norby on Nov 15, '02 12:35:22PM
  1. Use a .screenrc (in your home directory) to save your most used options, like this (I use ^W instead of ^A for reasons similar to the other poster, but use ^Z far too often to sacrifice it to screen): defscrollback 2000 vbell off startup_message off escape "^Ww"
  2. Use screen -x to connect to the same session from multiple places - you don't have to disconnect to re-connect from elsewhere.
  3. Screen has its own built-in scrollback. Use -[ to go into scroll mode, Ctrl-F/B go forward and back a page, and depending on whether your terminal is correctly setup or not, you should be able to use arrow keys, page-up/down, etc. Hit Enter to mark the beginning of a section to copy, then Enter again to mark the end. I think -] pastes, but I don't use it too often, so don't quote me on it. Check the man page for more details (man screen). -? for a key quick-ref while screen is running.
  4. Many terminal issues relating to color, scrollback, etc. can be tweaked by changing the value of the environment variable $TERM before or during your screen session (whether you need to change it before or after depends on what you're trying to accomplish (I'm not going to go into details, as they're too complex for me to understand, and therefore to pass along to anyone else)


[ Reply to This | # ]
Can't get screen to work, whats the deal?
Authored by: ElectusUnum on Nov 15, '02 10:05:57PM

I type screen and get this:

<i>[Mac:~] matt% screen
dyld: screen Undefined symbols:
_ospeed
Trace/BPT trap
[Mac:~] matt%</i>

Anyone know whats going on?



[ Reply to This | # ]
Can't get screen to work, whats the deal?
Authored by: mgarabed on Nov 18, '02 05:05:10PM

That's the error that I got when I would run screen and it defaulted to the Fink installed version. Explicitly calling/aliasing screen to /usr/bin/screen fixed the problem, of course as others noted that is also the older version than what fink installs.



[ Reply to This | # ]
Re: Can't get screen to work, whats the deal?
Authored by: thatch on Nov 19, '02 03:21:17AM

Sounds like your environment isn't right. If you want to use the fink installed screen, make certain you have 'source /sw/bin/init.csh' in your ~/.tcshrc.



[ Reply to This | # ]
screen not working in 10.2.2
Authored by: carbonfibre on Dec 04, '02 04:39:05PM

When I attempt to open screen from Terminal.app, I get the following error:

Cannot open your terminal '/dev/ttyp1' - please check.

Anyone know what this means?



[ Reply to This | # ]
yet another startup script
Authored by: smeger on Jan 22, '03 10:29:49PM
I'm using bash under 10.2.2. In ~/.bash_profile, I've got:
exec screen
# include .bashrc if it exists
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi
In ~/.bashrc, I've got:
# If running interactively, then:
if [ "$PS1" ]; then

    # set a fancy prompt
    PS1='El Smeg:\w\$ '

    # stuff for 'screen'
		# Login greeting ------------------
		if [ ! $SHOWED_SCREEN_MESSAGE ]; then
			detached_screens=`screen -list | sed -n 's/^\(.*\)(Detached)$/|\1|/gp'`
			if [ ! -z "$detached_screens" ]; then
				echo "+---------------------------------------+"
				echo "| Detached screens are available:       |"
				echo -n "$detached_screens"
				echo
				echo "+---------------------------------------+"
			else
				echo "+-----------------------------------------+"
				echo "[ There are no detached screens available ]"
				echo "+-----------------------------------------+"
			fi
			export SHOWED_SCREEN_MESSAGE="true"
		fi
fi
In NetInfo Manager, I've got my shell set to /bin/bash (and using 'chsh'). The result of all of this is that when I open a new terminal window, it automatically starts 'screen' and shows me whether I've got any existing open sessions. One last thing, supposedly you can use 'screen' to set the window title by the last command entered, but I can't get it to work properly. You can see more info searching 'man screen' for 'shelltitle'. I've got the following line in my ~/.bashrc:
    # set a fancy prompt
    PS1='El Smeg:\w\033k\033\134\$ '


[ Reply to This | # ]
Caution: 'exec screen' in .profile / bash
Authored by: victory on Jun 22, '06 03:41:36PM

Just a note on part 2b of the hint above:

If you're using bash as your default shell and you put the 'exec screen' line in your ~/.profile, this will prevent some apps from properly executing shell cmds/scripts.

For example, doing so prevents TextMate's 'Execute Line Inserting Result' command from working properly.

[TextMate is a popular programmer's text-editor from Macromates.com]
---
OSX 10.4.6
TextMate v1.5.2



[ Reply to This | # ]