screen-users
[Top][All Lists]
Advanced

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

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


From: Ryan Lynch
Subject: Automatically start/attach to a multi-user Screen session, for new BASH shell instances.
Date: Wed, 30 Sep 2009 11:59:17 -0400

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
address@hidden


##### 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.
# ...




reply via email to

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