2

I'm trying to write a script which spawns multiple long-running commands on both a remote and local machine. Currently, an example of my solution would be:

#!/bin/bash

ssh -t [email protected] tmux new -s remote -d "find / *"

tmux new -s local -d "find / *"

However, when these commands become more complex (long, chained commands with quotation marks in them, instead of just a single find command), it becomes messy to put them on a single line and escape all the nested quotation marks.

I have tried using SSH with a so called "here document", e.g.:

ssh -t [email protected] << EOF
    tmux new -s remote -d "find / *"
EOF

tmux new -s local -d "find / *"

But this leads to either the error not a terminal with the -T or -t option for SSH, or to some very strange behavior when using the -tt option.

Any suggestions to optimize/dramatically alter my approach?

2
  • A here-document redirects stdin, which is not the same thing as providing a command on the ssh (and ultimately tmux) command line. Are these commands largely fixed, such that a remote script file (possibly with a small number of parameters) may be a workable approach?
    – user
    Feb 7, 2017 at 13:04
  • Although these commands are mostly static, I'd like to maintain only 1 set of scripts instead of 2 (local + remote).
    – Girmi
    Feb 7, 2017 at 14:48

1 Answer 1

2

I think you should be able to combine this idea with this other idea and get something like this to work:

CMDS=$(cat <<CMD
read -e -p "Enter the path to the find: " FILEPATH
echo \$FILEPATH
#find \$FILEPATH -name $FILENAME
#read -p done: 
CMD
) 

tmux new -s finder -n remote "ssh localhost -t '$CMDS'" \; \
        new-window -n local  "bash -c '$CMDS'" \; \
            attach \;

Bonus - both commands are running in parallel and in the same tmux session.

Some quoting and escaping issues may remain depending upon the complexity of the commands you want to execute. Also note the read -p done or your commands will execute, terminate and the tmux will also terminate without you seeing the output. Perhaps that's what you intended, and the use of find was merely for example.

2
  • This seems like a good solution. I'll test it for my specific setup and report back. Just a small note: my intention is to make the SSH session run tmux on the remote host, instead of a local tmux session running SSH as your code does. This allow me to easily reconnect if the network is disconnected at some point. But that just applies to my specific case. And find was indeed just an example of a long running command.
    – Girmi
    Feb 14, 2017 at 12:39
  • Works like a charm!
    – Girmi
    Mar 2, 2017 at 14:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .