Auto-reattaching terminal multiplexor with tabs
by mmnicolas on Oct.21, 2009, under gnu/linux
Typically, from a single ssh connection comes a single shell, let’s improve this situation.
We will use a terminal multiplexor GNU Screen, which provide multiple terminals. We will also implement a neat tab bar to allow naming/showing tabs and automatic session re-attachment capability.
Follow these steps to significantly enhance your distant shell experience, you will need to install screen and ssh, mutt and irssi are optional.
The first step involves using environment variables in order to detect if the login is locally originating or not. If the user is logging from a distant machine, we will reattach an existing detached screen session or create a new one.
When logging in remotely, the most evident distinction i’ve found between a local and remote shell is the environment variable SSH_CONNECTION=, with this var we have a way of detecting remote logins easily and launch something else accordingly.
Insert in your ~/.bashrc:
if env | grep -q SSH_CONNECTION; then
. ~/.screenlogin.sh
fi
The previous lines will launch .screenlogin.sh upon a remote connection, a second script which will contain:
DETACHED=`screen -list | grep Detached | wc -l`
if [ “$DETACHED” = 1 ]; then
screen -DR
fi
Make the script executable:
chmod +x .screenlogin.sh
Now exit and login back again.
March 26th, 2008 on 14:00
Instead of:
This should also do the trick in a cleaner way:
May 17th, 2009 on 20:45
The self-reattachment is now a feature of earlier version of the screen package found in ubuntu and debian, with any newer version my trick is useless :’(
But if you use the latest LTS (8.04) or Lenny, this trick is pretty handy.