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

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

    There can be a case where the process is complete before waiting for the process. If we trigger wait for a process that is already finished, it will trigger an error like pid is not a child of this shell. To avoid such cases, the following function can be used to find whether the process is complete or not:

    isProcessComplete(){
    PID=$1
    while [ -e /proc/$PID ]
    do
        echo "Process: $PID is still running"
        sleep 5
    done
    echo "Process $PID has finished"
    }
    

提交回复
热议问题