3 ms·
Tangentially, anyone know how to run tmux (or screen) so that SSH sessions automatically attach? Ie., I want every interactive connection to automatically atta
by lobster_johnson 12y ago
Tangentially, anyone know how to run tmux (or screen) so that SSH sessions automatically attach?
Ie., I want every interactive connection to automatically attach to a session on the server if one exists, otherwise create one, so that when I disconnect or quit, my shell stays.
I tried to hack a solution using .zshrc a while ago, but it just didn't work properly and/or didn't handle all the edge cases, I forget exactly.
- ams6110 12y agoI have this in my .kshrc, can't recall where I found it. if [ "$PS1" != "" -a "${STARTED_TMUX:-x}" = x -a "${SSH_TTY:-x}" != x ] then STARTED_TMUX=1; export STARTED_TMUX sleep 1 ( (tmux has-session -t remote && tmux attach-session -t remote) || (tmux new-session -s remote) ) && exit 0 echo "tmux failed to start" fi I also don't recall why the "sleep" command is in there. Edit: to clarify, this is in the .kshrc of my work machine. When I start xterms on that machine, they just get a normal shell because $SSH_TTY is not set. When I ssh into my work machine from elsewhere, I get attached to the tmux session (if any) or one is started.
- lobster_johnson 12y agoThanks, I'll play around with this.
- pyre 12y agoWhat is the point of this: "${STARTED_TMUX:-x}" = x vs. -z "$STARTED_TMUX" or "$STARTED_TMUX" = "" It's always seemed like a legacy method of testing to empty string from a time when "" wouldn't be parsed as an argument. Is there any real reason to still do that today?
- positr0n 12y ago> I also don't recall why the "sleep" command is in there. Probably this issue? http://superuser.com/questions/629227/tmux-prints-12c-whenever-i-start-it-or-re-attach-to-a-session-in-cygwin http://superuser.com/questions/629227/tmux-prints-12c-whenev...
- Arubis 12y agoThe 'tmux' plugin for oh-my-zsh (https://github.com/robbyrussell/oh-my-zsh https://github.com/robbyrussell/oh-my-zsh) does this. If you want to avoid bloat you could just extract the script. EDIT: though I believe you'd need to have that on the server side.