Skip to content

Instantly share code, notes, and snippets.

@mthomas
Created December 20, 2016 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mthomas/ef9e1ebfa6435c7aefd2ee08ae0df2e0 to your computer and use it in GitHub Desktop.
Save mthomas/ef9e1ebfa6435c7aefd2ee08ae0df2e0 to your computer and use it in GitHub Desktop.
Run several commands in bash, in parallel. Let all complete regardless of return code. Return 1 if any fail.
#!/bin/bash
set -o errexit
set -o pipefail
pids=""
RESULT=0
(sleep 1; echo 'a'; exit 1) &
pids="$pids $!"
(sleep 2; echo 'b'; exit 0) &
pids="$pids $!"
(sleep 3; echo 'c'; exit 0) &
pids="$pids $!"
for pid in $pids; do
wait $pid || let "RESULT=1"
done
echo "done"
if [ "$RESULT" == "1" ];
then
exit 1
fi
exit 0
@mthomas
Copy link
Author

mthomas commented Dec 20, 2016

Goals...

  • be able to run with errexit option
  • be able to run a set of commands where some may fail
  • run them all in parallel
  • run them all to completion even if one fails first
  • exit the entire script with a non-zero exit code if any fail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment