April 30, 2016

Automatically attach to tmux when connecting via ssh

Recently I have needed to keep a long running sessions open on a server. And I have been using tmux for this purpose for quite some time. However, my internet connection has been flaky, so I’ve been dropping the connection very frequently. So I wanted to automate the steps. I also had the requirement that the server would have no custom configuration for tmux.

What I came up with was the following command:

ssh -t example.com "tmux attach-session -t chris || tmux new-session -s chris"

When I roll that up into a little bash script that will name the session the name of my user on my local machine:

#!/bin/bash
set -e

server=$1
if [ -z "$1" ] ; then
  echo "Usage: $(basename $0) hostname [session name]"
  exit 1
fi

name=$2
if [ -z "$2" ] ; then
  name=$(whoami)
fi

ssh -t $server "tmux attach-session -t $name || tmux new-session -s $name"


🚀