-
-
Save FoxxMD/6acdda15072c10d1a5311ebad0c1f1ef 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo | |
bar | |
baz |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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