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?


Solution 1:

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.