7

I'm looking at http://taint.org/wk/RemoteLoginAutoScreen to setup my server so it autostarts a screen session when I login via SSH.

I have added the following to my .bashrc:

# 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 [ "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x ]
then
  STARTED_SCREEN=1 ; export STARTED_SCREEN
  [ -d $HOME/lib/screen-logs ] || mkdir -p $HOME/lib/screen-logs
  sleep 1
  screen -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]

The catch is that I always have a detached named screen session running a rails application server. Now when I login I am put into this session.

Is there anyway to modify the above code to not select the session either by name or some other value? If the detached session is the only screen session then I would like to start a new screen session.

Another issue that I am encountering is that when I detach I am completely logged out of my SSH connection as opposed to just leaving the screen session.

1
  • Testing for an empty PS1 is a popular but broken way of checking that the shell is interactive. In particular, there are many systems where PS1 ends up an environment variable, set in every shell that you run. Use case $- in *i*) echo interactive;; *) echo not interactive;; esac or in bash [[ $- = *i* ]] to test for an interactive shell. Use [ -t 0 ] to test if standard input is a terminal. Sep 13, 2012 at 0:11

1 Answer 1

8

Change it to:

if [ -z "$STARTED_SCREEN" ] && [ -n "$SSH_TTY" ]
then
  case $- in
    (*i*)
      STARTED_SCREEN=1; export STARTED_SCREEN
      mkdir -p -- "$HOME/lib/screen-logs"
      screen -RR -S main  ||
        echo >&2 "Screen failed! continuing with normal bash startup"
  esac
fi

That is attach (or create) the screen session called "main" instead of the other one, and don't exit after screen has returned successfully.

3
  • Your suggestion works perfectly! Thank you. :) Sep 13, 2012 at 0:21
  • @Stephan Chazelas What is meaning of $- , when i use eacho then it's showing himBH, what it means ? thanks in advance for any help :) Dec 29, 2012 at 10:12
  • @RahulPatil It contains the current set of shell options (i for interactive (-i option)). See your shell manual for details. Dec 29, 2012 at 10:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .