How do I wait for several spawned processes?

There are numerous ways to do this, but all of them are either limited by the available tools. I have come up with the following solutions.

If you want to wait for all your children, simply call wait with no arguments.

If you just want to wait for some, but not all, and don't care about their exit status, you can call wait with multiple PIDs:

wait $pid1 $pid2

If you need to know whether the children succeeded or failed, then perhaps:

waitall() { # PID...
  ## Wait for children to exit and indicate whether all exited with 0 status.
  local errors=0
  while :; do
    debug "Processes remaining: $*"
    for pid in "$@"; do
      if kill -0 "$pid" 2>/dev/null; then
        debug "$pid is still alive."
        set -- "$@" "$pid"
      elif wait "$pid"; then
        debug "$pid exited with zero exit status."
        debug "$pid exited with non-zero exit status."
    (("$#" > 0)) || break
    # TODO: how to interrupt this sleep when a child terminates?
    sleep ${WAITALL_DELAY:-1}
  ((errors == 0))

debug() { echo "DEBUG: $*" >&2; }

for t in 3 5 4; do
  sleep "$t" &
  pids="$pids $!"
waitall $pids

Looping through kill -0 can be very inefficient.

More useful information might be found at Process Management page.

BashFAQ/108 (last edited 2011-09-12 23:03:28 by GreyCat)