I am impressed by tmux's remote control features:

tmux new-window -t p12346 'echo foo;sleep 10' 

This creates a new window in the session named p12346 and does not print anything on the screen. That is great. But how can I tell the above command to wait until the sleep finishes before returning? I.e. I want this do the same but to take 10 seconds to execute:

time tmux new-window -t p12346 'echo foo;sleep 10' 


If this is viable then it might become a feature of GNU Parallel: The jobs currently running can be accessed as each their window through tmux, so you can follow the progress of each job in real time.

So tmux will not be run in the foreground: The user will need to attach to it.


tmux 1.8 introduced the wait-for command that can help do what you want:

time tmux new-window -t p12346 'echo foo;sleep 10;tmux wait-for -S p12346-neww-done' \; \
          wait-for p12346-neww-done

The “channel” name (p12346-neww-done in the example above) can be anything you like (as long as it is the same in both places; if you anticipate multiple potentially simultaneous invocations, then you will probably also want to use a unique value for each invocation). The important part is that you “wait for” the signal after the new-window command (as a part of the same tmux command, thus the escaped semicolon: tmux needs to process it, not the shell) and to send the signal as the last step of the window’s command.

If you need to be a bit more robust against your command sequence aborting in an ugly or inconvenient way, then you might try using a shell trap to send the signal (rearranged a bit for readability(?)):

time \
tmux new-window -t p12346 '
       trap "tmux wait-for -S p12346-neww-done" 0
       echo foo
       sleep 10
       ' \; wait-for p12346-neww-done

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.