Autoconnect to your tmux session after sleeping

When working at multiple computers during the day there are a few methods of syncing your work. Using something like unison to share the actual code, or just work on the computer remotely through ssh. Since one of my current clients has a few vagrant boxes which are less than trivial to set up and keep in sync, working on a single box is the easiest solution.

In this particular case, my laptop. When I’m at home however, I prefer to use my regular workstation instead of my laptop (bigger screens, more comfortable, etc..) so I ssh into my laptop. Since I run all of my projects through tmux (with my tmx script: https://github.com/WoLpH/dotfiles/blob/master/bin/tmx), that’s easy enough. The only downside is that when my workstation goes to sleep (or the laptop does) the ssh connection inevitably dies.

Like any good programmer, I hate repetition and feel that this is something to be automated. So here’s a little zsh function (probably works in bash too) to automatically connect and reconnect to your tmux session:

function rtmx(){
    autossh -M $RANDOM HOSTNAME-OF-LAPTOP -t "PATH=/bin:/usr/bin:/usr/local/bin:~/bin; tmx $@"
}

The path is needed so the `tmx` and `tmux` commands can be found since opening a real “main” shell seemed pointless in this case.

You are required to install autossh, zsh, tmux and git for my dotfiles to work:

brew install autossh tmux zsh git

To install my dotfiles (don’t worry, it doesn’t overwrite anything, it just makes symlinks):

git clone https://github.com/WoLpH/dotfiles.git
cd dotfiles
./install.sh
Bookmark and Share

About Rick van Hattem

Rick van Hattem is a Dutch Internet entrepreneur and co-founder of Fashiolista.com
No comments yet.

Leave a Reply