I'm looking for a way to wait execution then detach from current tmux session through bash command line.

For example currently I'm trying to build a deployment automation command line tool. It should does:

  • create a new tmux session, attache to it,
  • run a file (e.g. build) showing user its stdout,
  • when its execution ends detach from the session,
  • and capture-pane to if the stdout is finished in a success way or an error way then if it's success is true continues to the next file (e.g. launch) and showing user its stdout creating and attaching to a new tmux session, and so on.

I tried the followings, however it didn't work as it doesn't wait for the previous commands but immediately detaches. (Note: assume the build.sh takes tens of seconds to execute with its stdout)

tmux a -t tmp \; \
    set-buffer ". /root/pg/myapp/production/.deploy/remote/build.sh;" \; paste-buffer \; send-keys C-m \; \
    set-buffer "tmux wait-for -S done;" \; paste-buffer \; send-keys C-m \; \
    set-buffer "tmux wait-for done;" \; paste-buffer \; send-keys C-m \; \
    detach \; 
tmux a -t tmp \; \
    set-buffer ". /root/pg/myapp/production/.deploy/remote/build.sh;" \; paste-buffer \; send-keys C-m \; \
    send-keys C-b \; \
    send-keys d \; 


# tmux -V
tmux 3.0a

# bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)


  • 1
    To be honest, that second paragraph with your "flow" is a bit hard to follow. I might suggest breaking that out into bullet points for clarity. Mar 12, 2022 at 2:26
  • fixed. thanks for the suggestion Mar 12, 2022 at 2:29

1 Answer 1


Solved the issue by the following:

tmux a -t tmp \; \
            set-buffer "sleep 1;" \; paste-buffer \; send-keys C-m \; \
            set-buffer "tmux detach;" \; paste-buffer \; send-keys C-m \;

Simply use tmux detach bash command instead of detach tmux command.

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.