How can I run complicated commands on establishing a mosh connection?

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!


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.

Tags:

Shell

Ssh

Tmux

Mosh