Getting the Screen Religion

After reading this excellent tutorial on using screen, I’ve started to use it more especially when on remote ssh connections. I wanted a way to invoke it automatically whenever I logged in, but only for remote sessions (locally I still have multiple terminal windows…) To do this paste the snippet below into your .bash_profile on the remote machine. Be sure to set up a .screenrc file as described in the above link – it makes a huge difference.

What this does is check to see if you are remote (whoami ends with (0.0.0.0) your IP)

Checks to see if there is one and only 1 screen session running – if so, resume – if more than one, list them. Otherwise create a new one. The reason I start screen with a named screen, in this case “start” is to bypass the intro screen that comes up otherwise.

AMREMOTE=`who am i | grep -c ")$"`
if [ ! -z $AMREMOTE ]; then
    SCREENS=`screen -list | head -1 | awk '{ print $1 }'`
    if [ $SCREENS = 'No' ]; then
        screen -t start
    else 
        SCREENCOUNT=`screen -list | tail -2 | head -1 | awk '{ print $1 }'`
        if [ $SCREENCOUNT -eq 1 ]; then
            screen -r
        else
            screen -list
        fi
    fi
fi

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment