I am trying to write a bash script that performs the following actions:
- open 8 tmux terminals
- each of the terminals connects in ssh to a remote server
- start a script X on 3 server, and when it's done
- start another script Y in 3 different servers
The operations (especially the last two) must be executed in this precise order. I managed to create all tmux terminals, but I see that for some reasons tmux is so fast at executing commands that it does not wait the command to finish.
For example, writing just:
tmux send-keys -t 0 "ls"
in the script not always produce the full list of files contained in the home folder.
Same happens with the two scripts on different servers, sometimes it executes Y before X.
This is my script:
######################################
# Setup tmux
######################################
tmux new-session -d -s sss
tmux new-window -t sss
for (( i = 0; i < 3; i++ )); do
tmux split
tmux select-layout even-vertical
done
for (( i = 0; i < 8; i=i+2 )); do
tmux select-pane -t $i
tmux split-window -h
done
######################################
######################################
# Ssh Connection
######################################
for (( i = 0; i < 8; i++ )); do
tmux send-keys -t $i "$connection ${port[$i]}" C-m
done
######################################
for (( i = 0; i < 3; i++ )); do
tmux send-keys -t $i "cd scripts" C-m
tmux send-keys -t $i "./X.sh" C-m
done
for (( i = 3; i < 6; i++ )); do
tmux send-keys -t $i "cd scripts" C-m
tmux send-keys -t $i "./Y.sh ${servers_ips[${i}-3]}" C-m
done
Update
Also, how can I wait for a command to finish in the same tmux pane? Like doing something else only after a long command has been running
Thank you in advance.