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?
ssh
when logging in? You must allocate a tty with-t
. Also, you must avoid startingtmux
if you're already in atmux
session (note that runningtmux
starts a new shell, which parses~/.bashrc
).if
-statements at all. The command substitution in your update does not make sense and would execute the output oftmux new-session
whenever that session terminated. There is otherwise no difference between the two codes. Also, consider usingexec
as when one of yourtmux
sessions exits, it would give you an interactivebash
shell (on the remote host) withoutexec
.if
check 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.