screen-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: open new shell on attach


From: Steven F. Killen
Subject: Re: open new shell on attach
Date: Wed, 16 Jun 2004 03:30:26 -0400 (EDT)

On Tue, 15 Jun 2004, Phil!Gregory wrote:

> * Andrew Leiserson <address@hidden> [2004-05-19 17:40 -0400]:
> > I would like to have screen open a new shell when I attach (from a newly
> > opened xterm).
>
> This is a question a friend of mine had recently, too, so I went poking at
> things.  I found one way to do this, provided you can come up with unique
> names for your screen windows.

Many thanks for your attention to it, phil.

> First off, you cannot create a new window and attach to screen in a single
> command, since screen's -X option trumps -r and -x.  (In fact, it changes
> the meaning of -r and just ignores -x.)  So you need to write a short
> script to create the window and then attach.
>
> <background; feel free to skip>...<skipped>
>
> You can, however, use the -p option to preselect a window.  -p takes
> either a number (which you can't always know) or a name.  So the trick is
> to create a unique name for your new window.  My /bin/sh (which is bash)
> provides $RANDOM, so I'd use
>
>   #/bin/sh
>   WINDOWNAME="newwin-$RANDOM"
>   screen -X screen -t $WINDOWNAME
>   screen -r -p $WINDOWNAME
>
> I don't know how POSIX $RANDOM is, so it might not exist everywhere.

This was exactly the approach I was going to take.  However, I beat on it
some, and it's not quite sufficient.  Some of the problems I solved, like
figuring out whether there was already a session running, and then
attaching correctly (-x vs. -r: FIGHT).  Where I've gotten is to be able
to run the "XTerm" button from the root menu in wmaker, which is aliased
to "xterm -fg white -bg black -e new-window".  It will add a new window to
the running session, and attach to it.

One of the minor complaints I have is that there's no obvious way to get a
window list outside of screen, so the script can't do any detection of
whether you want to be re-using windows or not.

However, my major beef is that when you start screen from a regular
terminal, typically you can say "screen programname" and it will create a
new window in the running session.  This does not appear possible in an
automated fashion; you have to already have screen running in the term
you're in, or you have to run screen from a non-screen'd term.  What I'd
really like to be able to do is to say something like:

xterm -bg black -fg white -e new-window "program -opt1 -opt2 ..."

And start up a new xterm, with the program specified in it, on its very
own screen, in the same session.

I tried a lot of different ways to tell it to behave:

screen progname opts... -X screen -t $WINDOWNAME
screen $ATTACH -p $WINDOWNAME

screen -X screen -t $WINDOWNAME
screen progname opts... $ATTACH -p $WINDOWNAME

screen -X screen -s "progname opts..." -t $WINDOWNAME
screen $ATTACH -p $WINDOWNAME

screen -X screen -s "tcsh; progname opts..." -t $WINDOWNAME

...

Just doing the obvious,

xterm -bg black -fg white -e screen -S $SESSION w3m freshmeat.net

ends up creating a new session with a lonely Web browser.

It sounds like at this point I need to dive into the source.  I was
thinking of adding an option to -ls that would include the resident
windows, which would give the script something to work with. Perhaps also
a -C option to accept an external command to run on $SHELL, that would
allow the window to stay open on the shell once the program stopped
running?

I welcome your thoughts, flames, and laughter.  My improvements to phil's
kind submission are below.

#!/bin/sh
#
# new-window
#
# Intended use:
# xterm [xterm-opts] -e new-window [title]
# as called by the root menu or dockapp.

SCREEN="/usr/local/bin/screen"
SESSION="`hostname -s`.`whoami`.xterms"
TITLE=${1:-"newwin"}
WINDOWNAME="$TITLE-$RANDOM"

until
IS_RUNNING="`$SCREEN -ls | grep $SESSION`" &&
STATUS=`expr "$IS_RUNNING" : '.*\(..tached\)'`
do
DEAD=`expr "$IS_RUNNING" : '.*\(Dead\)'`
if [ $DEAD ]; then echo "wiping screen..."; $SCREEN -wipe; fi
if [ ! $STATUS ]; then echo "starting screen in daemon mode..."; $SCREEN
-dmS $SESSION; fi
done

if [ $STATUS = "Detached" ]; then
        ATTACH="-r"
else
        ATTACH="-x"
fi

$SCREEN -X screen -t $WINDOWNAME
$SCREEN $ATTACH -p $WINDOWNAME

--
Steve Killen <address@hidden>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]