Wait for bash background jobs in script to be finished

后端 未结 4 1677
抹茶落季
抹茶落季 2020-12-05 03:53

To maximize CPU usage (I run things on a Debian Lenny in EC2) I have a simple script to launch jobs in parallel:

#!/bin/bash

for i in apache-200901*.log; do         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 04:21

    I had to do this recently and ended up with the following solution:

    while true; do
      wait -n || {
        code="$?"
        ([[ $code = "127" ]] && exit 0 || exit "$code")
        break
      }
    done;
    

    Here's how it works:

    wait -n exits as soon as one of the (potentially many) background jobs exits. It always evaluates to true and the loop goes on until:

    1. Exit code 127: the last background job successfully exited. In that case, we ignore the exit code and exit the sub-shell with code 0.
    2. Any of the background job failed. We just exit the sub-shell with that exit code.

    With set -e, this will guarantee that the script will terminate early and pass through the exit code of any failed background job.

提交回复
热议问题