How to wait in bash for several subprocesses to finish and return exit code !=0 when any subprocess ends with code !=0?

后端 未结 30 2816
悲哀的现实
悲哀的现实 2020-11-22 03:50

How to wait in a bash script for several subprocesses spawned from that script to finish and return exit code !=0 when any of the subprocesses ends with code !=0 ?

S

30条回答
  •  無奈伤痛
    2020-11-22 04:33

    Wait for all jobs and return the exit code of the last failing job. Unlike solutions above, this does not require pid saving. Just bg away, and wait.

    function wait_ex {
        # this waits for all jobs and returns the exit code of the last failing job
        ecode=0
        while true; do
            wait -n
            err="$?"
            [ "$err" == "127" ] && break
            [ "$err" != "0" ] && ecode="$err"
        done
        return $ecode
    }
    

提交回复
热议问题