Wait for program(s) in tmux panes to end…

I am doing quite a lot of work inside tmux. I especially love that I can start multiple windows/panes and use them to run the same thing across multiple servers, while still having normal shell access between steps.

This thing was greatly improved when I wrote tmux_send_to_many, but one thing was missing – waiting for the thing, running in tmux window to end.

Generally, I had to look at all the windows, and decide when to launch next step on my own.

Not anymore.

New script tmux_wait_for_many solves this problem (at least for me).

For example, let's assume that I run:

=$ tmux
=$ for t in test{1..3}; do tmux new-window -d -n $t; done
=$ tmux_send_to_many test 'sleep $(( RANDOM % 10 ))' Enter

and now, I'd like to launch something when all of these sleeps will end.

I can do it now trivially by running:

=$ tmux_wait_for_many test '^=\$[[:space:]]*$'

Like previously there are some options:

Syntax:
    tmux_wait_for_many [-a] [-f] [-l] [-v] [-s SKIP_MATCHER] [-w WAIT_TIME] PANE_MATCHER LINE_MATCHER
 
Description:
    Waits for line matching given regexp to appear in tmux pane(s).
    This is useful, for example, for checking whether command running in pane
    ended.
 
Options:
    -a : Enables matching across all sessions, not only current.
    -f : Match line in any place on screen, not only last non-empty.
    -l : List all matching panes and exit, don't wait on anything.
    -s : Skip lines that match this regular expression (egrep style).
    -w : Wait given time between checks of terminal, defaults to 1 (second).
    -v : Print verbose information while working.
 
Arguments:
    PANE_MATCHER    : Regular expression (compatible with egrep), that will be
                      used to select panes that should be processed.  Without
                      -a, panes are listed only from current session, and have
                      format:
                      WINDOW_NAME.PANE_INDEX
                      With -a, panes are listed from all sessions, and have
                      format:
                      SESSION_NAME:WINDOW_NAME.PANE_INDEX
    LINE_MATCHER    : Regular expression (compatible with egrep), that will be
                      used to check if last non-empty line in pane(s) contain
                      what was expected.
                      In case -f was provided, the whole screen (visible part)
                      will be checked.

and verbose mode to show you what's happening:

=$ tmux_send_to_many test 'sleep $(( RANDOM % 10 ))' Enter; time tmux_wait_for_many -v test '^=\$[[:space:]]*$'
Current tmux session: 1
Waiting for line matching: >>^=\$[[:space:]]*$<<
in the last line of:
test1.0:.+, test2.0:.....+, test3.0:...+
 
real    0m9.166s
user    0m0.104s
sys     0m0.079s

Not sure if you'll find it useful, but it is for me 🙂

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.