Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TechnologyClassroom/4aa84a1c929c8bb76acb391165bcb5d9 to your computer and use it in GitHub Desktop.
Save TechnologyClassroom/4aa84a1c929c8bb76acb391165bcb5d9 to your computer and use it in GitHub Desktop.
Put this into your .bashrc to auto-start a tmux session after SSH login
# Put these lines into your server's .bashrc:
##
## TMUX auto attach
##
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then # If this is an SSH session
if which tmux >/dev/null 2>&1; then # Check if tmux is installed
if [[ -z "$TMUX" ]] ;then # Do not allow "tmux in tmux"
ID="$( tmux ls | cut -d: -f1 | head -n 1 )" # Get the id of an existing session
if [[ -z "$ID" ]] ;then # If not available create a new one
tmux new-session
else
tmux attach-session -t "$ID" # If available, attach to it
fi
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment