Is it possible to several commands in the background but wait for all results and fail the script if an command fails Is it possible to several commands in the background but wait for all results and fail the script if an command fails shell shell

Is it possible to several commands in the background but wait for all results and fail the script if an command fails


Yes.

You can use the wait command in bash to wait for completion on one or more sub-processes to terminate in which case we provide the PID to wait on it. Also wait can optionally take no arguments in which case it waits for all background process to terminate.

Example:-

#!/bin/bashsleep 3 &wait "$!"     # Feeding the non-zero process-id as argument to wait command.              # Can also be stored in a variable as pid=$(echo $!)# Waits until the process 'sleep 3' is completed. Here the wait # on a single process is done by capturing its process idecho "I am waking up"sleep 4 &sleep 5 &wait          # Without specifying the id, just 'wait' waits until all jobs               # started on the background is complete.# (or) simply# wait < <(jobs -p)     # To wait on all background jobs started with (job &)echo "I woke up again"

Update:-

To identify the jobs when the fail, it is best to loop over the list of background jobs and log their exit-code for visibility. Thanks to wonderful suggestion by chepner. It goes like

#!/bin/bashfor p in $(jobs -p)do     wait "$p" || { echo "job $p failed" >&2; exit; }done


#!/bin/bashset -ebg(){    sleep .$[ ( $RANDOM % 10 ) + 1 ]s}bg2(){    sleep .$[ ( $RANDOM % 10 ) + 1 ]s    exit 1}export -f bgexport -f bg2parallel ::: bg bg2 || echo $? of the jobs failed