Alexander Mills
Alexander Mills

Reputation: 100010

wait -n ; is not waiting for all subshells to complete

I have this bash script:

#!/usr/bin/env bash


DIRN=$(dirname "$0")

OUTPUT_PATH=${PROJECT_ROOT:-$PWD}/npm-install-output.log

(cd $(dirname "$0")/one && echo $PWD && rm -rf node_modules ; npm --loglevel=warn --progress=false install) &
(cd $(dirname "$0")/two && echo $PWD && rm -rf node_modules ; npm --loglevel=warn --progress=false install) &
(cd $(dirname "$0")/three && echo $PWD && rm -rf node_modules ; npm --loglevel=warn --progress=false install) &

wait -n; echo "EXIT CODE => $?"

EXIT=$?

echo " all done with parallel installs "
echo " => bash exit code for script '$(dirname "$0")/$(basename "$0")' => $EXIT" &&
exit ${EXIT}

from my logging output, seems very clear that one of the subshells runs after the wait -n call.

How can I use wait or another construct such that I wait for all subshells to complete?

Upvotes: 1

Views: 6873

Answers (1)

hek2mgl
hek2mgl

Reputation: 157967

help wait is pretty clear about that:

If the -n option is supplied, waits for the next job to terminate and returns its exit status.

Use wait instead of wait -n


If you are interested in all three return values use:

for i in 1 2 3 ; do
    wait -n
    echo "exit code $?"
done

Upvotes: 9

Related Questions