Tmux is great. I use it all the time, but sometimes I forget to use it. When I'm in a hurry I forget to start a new tmux session until I'm tailing a log file and then I can't launch a new shell on a remote machine until I ssh again, or stop tailing, start tmux, redo the tail command.
I found a solution somewhere on StackOverflow that goes like this:
function ssh () {
/usr/bin/ssh -t "$@" "tmux -2 new -A -s ssh-session || tmux -2 attach || /bin/bash";
}
This changes your regular ssh command into a function. The function does the following.
- Passes all your arguments to the ssh program
- Tries to create a new session or resume (attach) to a session named ssh-session
- If that fails it tries to attach to any session (the first syntax doesn't work on old version of tmux (CentOS))
- If that fails then start bash because tmux is probably not installed
Using this bash function is just like using ssh normally.
ssh root@someserver
If you want to execute code remotely on the machine, then you'll want to reference the full path to the ssh binary.
/usr/bin/ssh root@someserver "tail -f /var/log/something"