Posts Tagged ‘HowTo’

Autorun Screen Sessions on an SSH Session

06 May

Source: Automatic ‘screen’ on remote logins

It annoys me whenever an SSH session I have to a server drops, and I can’t reconnect to the session, especially when there’s a long running tasks that I need to know the status of.

So anyway, the solution to this one is a project called GNU Screen. To summarize, it creates a virtual terminal session for you that you can actually resume on if ever your connection (or whatever) was terminated.

The problem with it is that you have to manually invoke the screen command when you successfully login, like so:

screen -D -RR

To run it on every SSH session, you can a script it to your bashrc profile. Either with your own bashrc located in ~/.bashrc or a global profile in /etc/bash.bashrc for all users. I normally put it for all users.

The script to add is found below, and is taken from

# Auto-screen invocation. see:
# 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 ]
  [ -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"
# [end of auto-screen snippet]
No Comments

Posted in HowTo