there is an answer here about tmux using wait-for and LocalCommand to wait until the SSH connection is established before continuing, but I needed a way to have tmux wait for the ssh session to be closed (after using send-keys to the remote host in multiple commands) before continuing.

LocalCommand doesn't do this.

here's the question:

1. opening a new tmux session
2. using send-keys to establish an ssh tunnel
3. sending multiple commands to the remote host using send-keys
4. closing the ssh tunnel using send-keys

How can I ensure bash waits until the terminal is ready to receive input before executing the next tmux send-keys command?

1 Answer 1


How I ended up solving this:

rm ~/sesh.lock 2> /dev/null    # in case of previous failure
tmux new-session -d -s sesh
tmux send-keys -t sesh "ssh prod-srv-1" Enter
tmux send-keys -t sesh "cd /srv/www/api/bin" Enter
tmux send-keys -t sesh "echo api stop > ~/api_log.txt" Enter
tmux send-keys -t sesh exit Enter

while [ ! -e ~/sesh.lock ]
    tmux send-keys -t sesh "touch ~/sesh.lock" Enter
    sleep 0.1
tmux send-keys -t sesh "rm ~/sesh.lock 2> /dev/null" Enter 

tmux kill-session -t sesh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.