I want tmux to start on ssh login.
The typical advice is to add this to ~/.bashrc:
if [ -z "$TMUX" ] && [ -n "$SSH_TTY" ] && [[ $- =~ i ]]; then
tmux attach-session -t mysession 2>/dev/null || tmux new-session -s mysession
fi
But when I log in, I get this error from tmux:
lost server
...And then I'm in bash as usual.
UPDATE
When I add a sleep:
if [ -z "$TMUX" ] && [ -n "$SSH_TTY" ] && [[ $- =~ i ]]; then
tmux attach-session -t mysession 2>/dev/null || $(sleep 1 && tmux new-session -s mysession)
fi
...then it works. Strange! What is the reason?
sshwhen logging in? You must allocate a tty with-t. Also, you must avoid startingtmuxif you're already in atmuxsession (note that runningtmuxstarts a new shell, which parses~/.bashrc). – Kusalananda Nov 17 '19 at 08:46if-statements at all. The command substitution in your update does not make sense and would execute the output oftmux new-sessionwhenever that session terminated. There is otherwise no difference between the two codes. Also, consider usingexecas when one of yourtmuxsessions exits, it would give you an interactivebashshell (on the remote host) withoutexec. – Kusalananda Nov 17 '19 at 09:09ifcheck to give context, I left it out initially to keep it simple. I didn't realise it was having an effect, as the error was the same regardless. Thanks again. – lonix Nov 17 '19 at 09:13