Automatically start/attach to a multi-user Screen session, for new BASH shell instances.

View: New views
4 Messages — Rating Filter:   Alert me  

Automatically start/attach to a multi-user Screen session, for new BASH shell instances.

by Ryan B. Lynch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've set up my .bashrc to automatically attach me to a Screen session
when I open a new BASH shell (unless the env var $STY is set, or $TERM
contains 'screen', or the shell isn't interactive).  I have some
wrapper logic, but the basic algorithm is:

 * Always use '__DEFAULT' as a Screen session name.
 * Each new BASH shell / Screen window gets the logon date/time, as a
window title.
 * At logon, attach to a new window, in the existing Screen session
(if it exists) or in a newly-created session (if none exist, already).
     * Try to execute ('-X' opt) the command `eval "screen -t
$DATETIME" in an existing session named '__DEFAULT' (returns nozero,
if no such session).
     * If there is no existing session (previous command failed),
create a new, detached session ('-dm' opts), with a single window ('-t
$DATETIME' opt).
     * Attach as a multi-user ('-mRR' opts) to the now-existing
session '__DEFAULT', to the window '$DATETIME'.

This is pretty nice, for me--I don't have to remember to attach each
new BASH instance to a Screen session, every time I open a new tab in
'konsole' or logon via a VT or SSH.  But it feels kind of complicated,
to me.

Is there an easier way to do this?  I've included an abridged version
of my .bashrc, below, in case it's not clear from the description.  I
would be very interested in hearing from anyone else who has tried
anything similar:  What do you do, and how do you do it?

Thanks,
Ryan

Ryan B. Lynch
ryan.b.lynch@...


##### From .bashrc #####

# Some checks for the current BASH's interactive-ness, $TERM, and $STY.
# ...
# A short-timeout warning message that we're about to enter a Screen
session, or hit any key to drop into a normal BASH shell.
# ...

SCREEN_SESSION_NAME="__DEFAULT"
SCREEN_DATETIME="$(date +%Y%m%d_%H%M%S)"

# Add a new window, if a session already exists. If not, create a
window in a new, detached session.
if ! /usr/bin/screen -xRR "${SCREEN_SESSION_NAME}" -q -X eval "screen
-t ${SCREEN_DATETIME}"; then
    /usr/bin/screen -dmS "${SCREEN_SESSION_NAME}" -q -t ${SCREEN_DATETIME}
fi

# Co-operatively re-attch to whatever session, existing or created just now.
/usr/bin/screen -xRR "${SCREEN_SESSION_NAME}" -p ${SCREEN_DATETIME}

# A short-timeout warning message that we're about exit this BASH
shell, or hit any key to continue in it, instead.
# ...


_______________________________________________
screen-users mailing list
screen-users@...
http://lists.gnu.org/mailman/listinfo/screen-users

Re: Automatically start/attach to a multi-user Screen session, for new BASH shell instances.

by David-723 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a very similar setup...   It is so nice to be able to login directly at my computer, or SSH, or open an xterm, etc and always be placed inside a pre-existing screen session, able to access all my other screen windows.

Here are links to my .bashrc and .bash_login that I use to make this happen.   More of my notes are  http://tech.thedesignhut.net/screen


My code is not formatted the best.   I have a fair amount of comments along the way in each file, detailing the screen-related issues for dealing with logins that happen in situations like SCP and x-ming, and if x-windows is running from within screen and then you open a xterm w/in that, etc.

Took me lots of trial and error, am open to any input for improving those files.   They do work great currently.

The main screen session is "Cornerstone" (with one blank, non-writable window, has a message giving its purpose, and is held open with a long "sleep" command) and windows are titled by date & time.   In case two logins happen during the same minute or second, milliseconds is also used in the window name,  in order to make sure each window name is unique.

Looking through your e-mail...  perhaps I don't need to have the "Cornerstone" window held open all the time, it can recreated each time it's needed.   The names of the windows are quite long b/c they include the milliseconds;  I'd like them to be shorter because my .screenrc makes each window to be listed in the caption, and I can't see more than two window titles in my caption since each window title is so long -- BUT I still want to guarantee uniqueness of window titles.

In my script, the DISPLAY variable is intensionally imported into each screen window individually, this is necessary for x-ming (or X-over-SSH) to work.

-David




2009/9/30 Ryan Lynch <ryan.b.lynch@...>
I've set up my .bashrc to automatically attach me to a Screen session
when I open a new BASH shell (unless the env var $STY is set, or $TERM
contains 'screen', or the shell isn't interactive).  I have some
wrapper logic, but the basic algorithm is:

 * Always use '__DEFAULT' as a Screen session name.
 * Each new BASH shell / Screen window gets the logon date/time, as a
window title.
 * At logon, attach to a new window, in the existing Screen session
(if it exists) or in a newly-created session (if none exist, already).
    * Try to execute ('-X' opt) the command `eval "screen -t
$DATETIME" in an existing session named '__DEFAULT' (returns nozero,
if no such session).
    * If there is no existing session (previous command failed),
create a new, detached session ('-dm' opts), with a single window ('-t
$DATETIME' opt).
    * Attach as a multi-user ('-mRR' opts) to the now-existing
session '__DEFAULT', to the window '$DATETIME'.

This is pretty nice, for me--I don't have to remember to attach each
new BASH instance to a Screen session, every time I open a new tab in
'konsole' or logon via a VT or SSH.  But it feels kind of complicated,
to me.

Is there an easier way to do this?  I've included an abridged version
of my .bashrc, below, in case it's not clear from the description.  I
would be very interested in hearing from anyone else who has tried
anything similar:  What do you do, and how do you do it?

Thanks,
Ryan

Ryan B. Lynch
ryan.b.lynch@...


##### From .bashrc #####

# Some checks for the current BASH's interactive-ness, $TERM, and $STY.
# ...
# A short-timeout warning message that we're about to enter a Screen
session, or hit any key to drop into a normal BASH shell.
# ...

SCREEN_SESSION_NAME="__DEFAULT"
SCREEN_DATETIME="$(date +%Y%m%d_%H%M%S)"

# Add a new window, if a session already exists. If not, create a
window in a new, detached session.
if ! /usr/bin/screen -xRR "${SCREEN_SESSION_NAME}" -q -X eval "screen
-t ${SCREEN_DATETIME}"; then
   /usr/bin/screen -dmS "${SCREEN_SESSION_NAME}" -q -t ${SCREEN_DATETIME}
fi

# Co-operatively re-attch to whatever session, existing or created just now.
/usr/bin/screen -xRR "${SCREEN_SESSION_NAME}" -p ${SCREEN_DATETIME}

# A short-timeout warning message that we're about exit this BASH
shell, or hit any key to continue in it, instead.
# ...


_______________________________________________
screen-users mailing list
screen-users@...
http://lists.gnu.org/mailman/listinfo/screen-users


_______________________________________________
screen-users mailing list
screen-users@...
http://lists.gnu.org/mailman/listinfo/screen-users

Parent Message unknown Re: Automatically start/attach to a multi-user Screen session, for new BASH shell instances.

by Ryan B. Lynch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

(CCing the Screen users list, in case anybody else is interested in this, too.)

On Tue, Oct 6, 2009 at 18:19, Ryan Lynch <ryan.b.lynch@...> wrote:
> When I've cleaned it up enough for human consumption, I'll post a copy
> of the whole script.

I've attached a copy of the script I'm currently using to auto-join
Screen when I start a shell.  I call it 'Porch'.  Here's a pretty
comprehensive feature list:

 * All new BASH instances will join the same screen session.  If it
doesn't exist, the first shell will start it, and then join.
 * All shells join the session co-operatively (the '-m' flag), so that
each new joiner doesn't kick the rest off.
 * Auto-exits the shell when Screen terminates, or when the display detaches.
 * Allows a short timeout, with a PRESS ANY KEY TO CANCEL type message
displayed, for the user to cancel joining the session or exiting the
shell when the session terminates/detaches.
 * If you manually cancel out of joining a Screen session, it
remembers, and exports a flag to sub-shells that supresses the
auto-joining behavior.
 * Ignores non-interactive shells.
 * Ignores already-'Screened' shells. (With an exception, see below.)
 * Inserts the Screen session name into your "hardstatus" line,
automatically, by modifying BASH's $PROMPT_COMMAND.  (This normally
isn't possible because Screen's own 'hardstatus string ...' command
doesn't support an escape for the session name.)

There are some broken bits that I haven't had time to finish
debugging, though.  I'm trying to make it nest properly, so that you
can join a local Screen session, and then run an SSH client from
within the local session that connects to a remote host, and then join
a remote Screen session.  (This requires installing the script on both
the local and remote hosts, of course.)

But for now, it works well enough to be useful.  I never have to
regret not starting a Screen session, earlier.

I'm making this available as-is, in case anyone wants to use it, copy
it, or just get ideas from it.  I would appreciate any suggestions,
feedback, or patches, too.

To use it, save a copy locally (I stick it in '/usr/bin'), and source
it at the end of your .bashrc, like this:

. /usrbin/porch.sh

-Ryan


_______________________________________________
screen-users mailing list
screen-users@...
http://lists.gnu.org/mailman/listinfo/screen-users

porch.sh (10K) Download Attachment

Re: Automatically start/attach to a multi-user Screen session, for new BASH shell instances.

by Ryan B. Lynch :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Oct 9, 2009 at 19:34, Ryan Lynch <ryan.b.lynch@...> wrote:
> (CCing the Screen users list, in case anybody else is interested in this, too.)
>
> On Tue, Oct 6, 2009 at 18:19, Ryan Lynch <ryan.b.lynch@...> wrote:
>> When I've cleaned it up enough for human consumption, I'll post a copy
>> of the whole script.
>
> I've attached a copy of the script I'm currently using to auto-join
> Screen when I start a shell.  I call it 'Porch'.  Here's a pretty
> comprehensive feature list:

Also, you probably want to edit the '#!/bin/bash -x' line, and remove
the '-x' option.  That was just for debugging.

-Ryan


_______________________________________________
screen-users mailing list
screen-users@...
http://lists.gnu.org/mailman/listinfo/screen-users