Advertisment

Automatically start SCREEN on SSH login

I just found a nice method to invoke screen automatically if after login via SSH. So you don't have to manually start it first.

Put the following at the end of your ~/.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]

If you want more details and some additional tricks just go to http://taint.org/wk/RemoteLoginAutoScreen.

4 comments to Automatically start SCREEN on SSH login

  • paz

    Merci für den Tipp!

  • Hey, I noticed that if you use this script, then you can’t create new screen windows with Ctrl+A, C. When you do that, it starts with a fresh environment so STARTED_SCREEN is not set. The solution is to test if screen is not already started with $TERM != “screen”

    If you use the following line, it works better:

    if [ “$PS1″ != “” -a $TERM != “screen” -a “${STARTED_SCREEN:-x}” = x -a “${SSH_TTY:-x}” != x ]

  • Lee

    I tested the code above on Ubuntu 12.04, 12.10, 13.04, Debian 6 and 7, Arch 2012.12.01, and OpenWRT bleeding edge.

    ^A, C worked just fine on all of those. I’m not sure why Martijn’s would be different.

  • carl

    I added the code to the bottom of .bashrc on raspbian wheezy. The system’s response upon sourcing the file was to clear the terminal’s display and present me with a blinking cursor, as if .bashrc was never finished. I had to ctrl-c to regain the prompt. I moved the code to the top of the .bashrc with the same result. I am unsure how to troubleshoot the problem. My original .bashrc can be examined at this link:

    http://pastebin.com/4M5QgkJE

    Thanks for any enlightenment.
    Carl

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>