Random Notes‎ > ‎

Automatically start tmux on SSH login

posted Apr 16, 2011, 10:15 PM by William Shallum   [ updated Oct 2, 2014, 1:12 AM ]
tmux is a terminal multiplexer, much like screen. Here is how to start it on SSH login (adapted from the script for screen at http://taint.org/wk/RemoteLoginAutoScreen):

if [ "$PS1" != "" -a "${STARTED_TMUX:-x}" = x -a "${SSH_TTY:-x}" != x ]
        sleep 1
        ( (tmux has-session -t remote && tmux attach-session -t remote) || (tmux new-session -s remote) ) && exit 0
        echo "tmux failed to start"

Put it somewhere in your .bashrc. This will make all SSH logins default to the same session (remote). If you close the final window in that session, the session will be closed and all the clients connected to that session will exit. You can attach the client to another session by using attach-session.