Skip to content

Instantly share code, notes, and snippets.

@wndhydrnt
Last active May 22, 2024 15:59
Show Gist options
  • Save wndhydrnt/3be3cd4552ec356f98e7497b0d4a426c to your computer and use it in GitHub Desktop.
Save wndhydrnt/3be3cd4552ec356f98e7497b0d4a426c to your computer and use it in GitHub Desktop.
Run several commands in parallel, prefix the stdout of each stdout with its source and fail the script if one of the commands exits with a code != 0
foo
bar
baz
#!/usr/bin/env bash
# Example:
# $ bash ./main.sh
# [baz] Sun Aug 11 12:05:28 CEST 2019
# [foo] Sun Aug 11 12:05:28 CEST 2019
# [bar] Sun Aug 11 12:05:28 CEST 2019
# [foo] Sun Aug 11 12:05:31 CEST 2019
# [baz] Sun Aug 11 12:05:31 CEST 2019
# [foo] exiting
# [baz] Sun Aug 11 12:05:34 CEST 2019
# [bar] Sun Aug 11 12:05:37 CEST 2019
# [baz] Sun Aug 11 12:05:37 CEST 2019
# [baz] Sun Aug 11 12:05:40 CEST 2019
# [baz] exiting
# [bar] Sun Aug 11 12:05:46 CEST 2019
# [bar] Sun Aug 11 12:05:55 CEST 2019
# [bar] Sun Aug 11 12:06:04 CEST 2019
# [bar] exiting
# $ echo $?
# 1
pids=""
RESULT=0
for l in $(cat list); do
# 'bash -eo pipefail -c' allows for capturing the correct exit code.
# It is needed because the output of ./program.sh is piped through awk to prefix the output.
# The pipe makes the line to always exit with a code of 0 because 'awk' always succeeds without -o pipefail.
bash -eo pipefail -c "./program.sh | awk '{print \"[${l}] \"\$0}'" &
pids="${pids} $!"
done
# 'jobs -p' might work as well
for pid in $pids; do
# 'wait' is nice and returns the exit code of a process even if the process has exited already!
wait $pid || let "RESULT=1"
done
exit ${RESULT}
#!/usr/bin/env bash
iterations=$((1 + RANDOM % 10))
pause=$((1 + RANDOM % 10))
exit_code=$((RANDOM % 2))
for (( i=1; i<=${iterations}; i++ ))
do
date
sleep ${pause}
done
echo "exiting ${exit_code}"
exit ${exit_code}
#!/usr/bin/env bash
# Same output as "main.sh" but using xargs.
processes=$(cat ./list | wc -l)
cat ./list | xargs -I{} -P ${processes} bash -eo pipefail -c "./program.sh | awk -W interactive '{print \"[{}] \"\$0}'"
echo "xargs exit code: $?"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment