Autoscreen

screen” is a fantastic Linux command-line application which allows you to start, disconnect, and resume multiple shell sessions. Typically used on a remote machine via SSH.

The minor annoyance is that it adds yet another step to connecting to your machine, so here is a script (nicked from here) which you can include (or source from) your “.profile” to automatically start or resume a screen session.

RemoteLoginAutoScreen script
# 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.
#
# NB: If you have a DISPLAY variable set (eg, ssh -X) then this script will
# not start screen automatically. If you want this to happen, remove the
# check.
if [ "$TERM" != screen -a "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x -a "$DISPLAY" == "" ]
then
echo "Auto-starting screen."
 
# Set the window title to HOSTNAME
echo -ne "\e]2;$HOSTNAME\a"
 
# If no session is running, set DISPLAY environment variable
screen -ls | egrep "^No Sockets found" > /dev/null
if [ $? = 0 ]; then
export DISPLAY=:$(( $( (echo 5555; ls /tmp/.X11-unix/X* 2> /dev/null) | sed 's/^.*\/X//' | sort -n | tail -n 1) + 1))
echo "No running screen found. DISPLAY set to $DISPLAY."
fi
STARTED_SCREEN=1 ; export STARTED_SCREEN
screen -D -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]

 

Personally I source the script, so I save it as “.autoscreenrc” and add the following to my “.profile”:

.profile snippet
# Auto-start screen
if [ -f "$HOME/.autoscreenrc" ] ; then
    . "$HOME/.autoscreenrc"
fi

 

And bingo, this saves you all of .01 seconds when you login 😉