Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Gilles Sep 13 '12 at 0:11

1 Answer 1

up vote 5 down vote accepted

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 "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.

share|improve this answer
    
Your suggestion works perfectly! Thank you. :) –  KayakJim Sep 13 '12 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 :) –  Rahul Patil Dec 29 '12 at 10:12
    
@RahulPatil It contains the current set of shell options (i for interactive (-i option)). See your shell manual for details. –  Stéphane Chazelas Dec 29 '12 at 10:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.