Tuesday, June 18, 2013

Automatically start tmux on ssh connection. Logout on detach.

This bit automatically opens tmux when ssh'ing into a server. If a session does not exist a new one is created. Otherwise, the last session is attached. This should always be run last either in .bashrc or as a script in .bashrc.d. The user is logged out from the server on detach.
# This should always be run last either in .bashrc or as a script in .bashrc.d
if [[ -z "$TMUX" ]]; then
    tmux has-session &> /dev/null
    if [ $? -eq 1 ]; then
      exec tmux new
      exit
    else
      exec tmux attach
      exit
    fi
fi