Archive for the ‘bash’ Category

Run ‘screen’ and reconnect to the same session without arguments

Sunday, September 16th, 2007

This is just a little nugget I’ve had around for a while. I’m dusting it off and posting here more to test the WP-Syntax WordPress Plugin than any other reason.

I use screen, but I don’t leave myself logged in when I’m not at my computer. I can usually type ‘screen -R’ to reconnect to the same session the next day, but if I have more than one screen session running at once, I need to ‘screen -ls’, then figure out which one to reattach to. At least I think I need to do that… in addition to being to lazy to do all that, I’m too lazy to scour the man pages looking for the way to make it create or reattach to the same session every time.

If you call ‘screen’ with parameters, they will be used instead… This should allow you to use screen completely normally when necessary.

#!/bin/sh
# Rename your screen binary to something else to get it out of
# the way, and save this script as 'screen' into the same
# directory. Make a note of what you called the original here:
SCREEN="/usr/local/bin/screen1"

# Make $DEFAULT_NAME whatever you want; you'll never actually see it:
DEFAULT_NAME="default_session"

if [ "$#" -eq 0 ]; then
    SCREENLS=`$SCREEN -ls | grep "$DEFAULT_NAME"`
    if [ "$SCREENLS" = "" ]; then
        $SCREEN -S "$DEFAULT_NAME"
    else
        $SCREEN -r "$DEFAULT_NAME"
    fi
else
    $SCREEN $@
fi