Question

With ssh I can do this:

ssh REMOTE -t 'tmux a || tmux'

With mosh the best I can do is this:

mosh REMOTE -- tmux a

But this won't work:

mosh REMOTE -- tmux a || tmux

Neither does this: (It doesn't matter whether it's single quote or double quote, I tried both)

mosh REMOTE -- 'tmux a || tmux'

So, my question is: How am I supposed to do this job?

Was it helpful?

Solution

Well, it seems that I have to explicitly use a shell to execute command:

mosh REMOTE -- sh -c 'tmux a || tmux'

EDIT

Instead of doing tmux a || tmux, a better way is add new-session to ~/.tmux.conf and just run tmux. That would make things much easier. I can do things like this now:

mosh REMOTE -- tmux

Awesome!

OTHER TIPS

There might be more complicated commands than the examples given above. I wanted to make a command that reattaches to an existing tmux session if one exists but is not already attached, or a new one if there are none available.

Looking at this example, I would have done something like this:

function tmosh() {
    mosh $1 -- (tmux ls | grep -vq attached && tmux at -t $( tmux ls | grep -vm1 attached | cut -d: -f1 ) ) || tmux new
}

But that doesn't work, per the original question above.

My solution so far is to have a wrapper script on the host servers:

tmux-reattach-if-exists

which consists simply of:

(tmux ls | grep -vq attached && tmux at -t $( tmux ls | grep -vm1 attached | cut -d: -f1 )) || tmux new

Then I used called the script on the client from mosh like this:

function tmosh() {
    mosh $1 -- tmux-reattach-if-exists
}

If there's a solution that can do this via .tmux.conf directly that would be great but I couldn't seem to work that out.

Put this at the end of your .bashrc

s1="`ps $PPID|grep mosh|awk '{print $5}'`"
s2=mosh-server
if [[ "$s1" == "$s2" ]]; then source .moshrc; fi

If called by mosh-server, bash will execute whatever it finds in $HOME/.moshrc - so just put your commands in a file named .moshrc in your home directory.

Because mosh invokes a login shell, you should have a line

source .bashrc

in your .bash_profile, or put the above lines in .bash_profile.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top