I'm looking at http://taint.org/wk/RemoteLoginAutoScreen to setup my server so it autostarts a screen session when I login via SSH.
I have added the following to my .bashrc:
# Auto-screen invocation. see: http://taint.org/wk/RemoteLoginAutoScreen
# if we're coming from a remote SSH connection, in an interactive session
# then automatically put us into a screen(1) session. Only try once
# -- if $STARTED_SCREEN is set, don't try it again, to avoid looping
# if screen fails for some reason.
if [ "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x ]
then
STARTED_SCREEN=1 ; export STARTED_SCREEN
[ -d $HOME/lib/screen-logs ] || mkdir -p $HOME/lib/screen-logs
sleep 1
screen -RR && exit 0
# normally, execution of this rc script ends here...
echo "Screen failed! continuing with normal bash startup"
fi
# [end of auto-screen snippet]
The catch is that I always have a detached named screen session running a rails application server. Now when I login I am put into this session.
Is there anyway to modify the above code to not select the session either by name or some other value? If the detached session is the only screen session then I would like to start a new screen session.
Another issue that I am encountering is that when I detach I am completely logged out of my SSH connection as opposed to just leaving the screen session.
PS1
is a popular but broken way of checking that the shell is interactive. In particular, there are many systems wherePS1
ends up an environment variable, set in every shell that you run. Usecase $- in *i*) echo interactive;; *) echo not interactive;; esac
or in bash[[ $- = *i* ]]
to test for an interactive shell. Use[ -t 0 ]
to test if standard input is a terminal.