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

后端 未结 30 2476
悲哀的现实
悲哀的现实 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:36

    I think that the most straight forward way to run jobs in parallel and check for status is using temporary files. There are already a couple similar answers (e.g. Nietzche-jou and mug896).

    #!/bin/bash
    rm -f fail
    for i in `seq 0 9`; do
      doCalculations $i || touch fail &
    done
    wait 
    ! [ -f fail ]
    

    The above code is not thread safe. If you are concerned that the code above will be running at the same time as itself, it's better to use a more unique file name, like fail.$$. The last line is to fulfill the requirement: "return exit code 1 when any of subprocesses ends with code !=0?" I threw an extra requirement in there to clean up. It may have been clearer to write it like this:

    #!/bin/bash
    trap 'rm -f fail.$$' EXIT
    for i in `seq 0 9`; do
      doCalculations $i || touch fail.$$ &
    done
    wait 
    ! [ -f fail.$$ ] 
    

    Here is a similar snippet for gathering results from multiple jobs: I create a temporary directory, story the outputs of all the sub tasks in a separate file, and then dump them for review. This doesn't really match the question - I'm throwing it in as a bonus:

    #!/bin/bash
    trap 'rm -fr $WORK' EXIT
    
    WORK=/tmp/$$.work
    mkdir -p $WORK
    cd $WORK
    
    for i in `seq 0 9`; do
      doCalculations $i >$i.result &
    done
    wait 
    grep $ *  # display the results with filenames and contents
    

提交回复
热议问题