Skip to content

Instantly share code, notes, and snippets.

Created May 10, 2013 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5557337 to your computer and use it in GitHub Desktop.
Save anonymous/5557337 to your computer and use it in GitHub Desktop.
# 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 [ "$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 0; ls /tmp/.X11-unix/X* 2> /dev/null) | sed 's/^.*\/X//' | sort | 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]
@libcrack
Copy link

##[--------------- start csh auto-screen snippet ----------------]
# BSD flavour by borja@libcrack.so - 2015
# 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.
setenv STARTED_SCREEN
setenv DISPLAY
alias settitle printf '"\033]2;\!*\a\033]1;\!*\a\033]0;\!*\a"'
if ( "$TERM" != screen && $?prompt == 1 && "${STARTED_SCREEN}X" == X \
            && "${SSH_TTY}X" != X && "${DISPLAY}X" == X ) then
  echo "Auto-starting screen."
  # Set the window title to HOSTNAME
  settitle $user@$HOSTNAME : $cwd
  # If no session is running, set DISPLAY environment variable
  screen -ls | egrep -q "^No Sockets found"
  if ( $? == 0 ) then
    setenv | grep -q DISPLAY || setenv DISPLAY 0
    @ DISPLAY = $DISPLAY + 1
    setenv DISPLAY $DISPLAY
    echo "No running screen found. DISPLAY set to $DISPLAY."
  endif
  sleep 3
  setenv STARTED_SCREEN 1
  screen -D -RR && exit 0
  # normally, execution of this rc script ends here...
  echo "Screen failed! Continuing with normal bash startup."
endif
if  ( "$STARTED_SCREEN"X == X && "${prompt}"X != X ) then
    echo "Available screens:"
    screen -ls
endif
##[------------------ end csh of auto-screen snippet ------------------]

@libcrack
Copy link

Here it is the the csh script flavour for the *BSD lovers ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment