The way I have things set up I launch screen on every server I connect to if it has screen. When I disconnect and reconnect, the same screen session is reconnected. Sounds good so far, that's what everyone wants. But, if you have several windows open in screen, how do you disconnect without closing them? There are 2 options:
- My preferred ssh client will disconnect if I hit "`." and in my screenrc I use "autodetach", but that only works remotely.
- For local (or remote) screens I have my bashrc set to disconnect if I detach from screen.
However, in rare cases you want to detach and stay connected. For that, I have some magic in my .bash_logout to complement my .bashrc magic.
.screenrc
hardstatus alwayslastline
hardstatus string '%{= kG} %{G}%H %{g}[%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}] %{W}%c %{g}'
#dynamic title
shelltitle '# |sh'
#When your ssh connection dies, screen is autodetached
autodetach on
#terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I"
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm "Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm"
termcapinfo xterm-color "Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm"
# erase background with current bg color
defbce "on"
.bashrc
# set a fancy prompt (non-color, unless we know we "want" color)
if [[ $TERM =~ xterm-.*color || $TERM =~ screen.* ]]; then
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]# '
#if [[ $TERM =~ screen.* ]]; then
export SCREEN_CMD=$(which screen 2>/dev/null)
if [[ ( $TERM =~ screen.* ) || ${SCREEN_CMD-X} != X && ${SCREEN_CMD-X} != "" ]]; then
# This is the escape sequence ESC k \w ESC \
#Use path as title
#SCREENTITLE='\[\ek\w\e\\\]'
#Use program name as title
SCREENTITLE='\[\ek\e\\\]'
else
#Soliton@freenode#screen suggested screen -xRRS primary
echo ^[k$(hostname|sed "s/\..*//")^[\\
export SCREEN_CMD=$(which screen 2>/dev/null)
if [[ ${SCREEN_CMD-X} != X && ${SCREEN_CMD-X} != "" ]]; then
screen -xRRS primary && unset SCREEN_CMD && [[ $(stat -c %Y .screen_do_not_disconnect 2>/dev/null || stat -f %m .screen_do_not_disconnect 2>/dev/null) -gt 0 ]] || exit
fi
fi
else
PS1='\u@\h:\w# '
SCREENTITLE=''
#Soliton@freenode#screen suggested screen -xRRS primary
echo ^[k$(hostname|sed "s/\..*//")^[\\
export SCREEN_CMD=$(which screen 2>/dev/null)
if [[ ${SCREEN_CMD-X} != X ]]; then
screen -xRRS primary && unset SCREEN_CMD && [[ $(stat -c %Y .screen_do_not_disconnect 2>/dev/null || stat -f %m .screen_do_not_disconnect 2>/dev/null) -gt 0 ]] || exit
fi
fi
PS1="${SCREENTITLE}${PS1}"
.bash_logout
# ~/.bash_logout
time="$(TZ=UTC date -d @0 2>/dev/null||date -r 0 +%Y%m%d%H%M)"
touch -d "$time" ~/.screen_do_not_disconnect 2>/dev/null || touch -t "$time" ~/.screen_do_not_disconnect
The scripts above work on both Linux and Mac OS X (which is the reason you see "2>/dev/null ||" in the stat, touch, and date commands). I also use a 256 color xterm and I believe all the supporting code is included here. Despite the length of this post, I have trim a lot out of the scripts/configs.