Last active
October 28, 2015 12:49
-
-
Save grunka/2ab6e7cd6bb74243baab to your computer and use it in GitHub Desktop.
One way of launching simultaneous jobs in bash and then waiting for them to complete and also get logs
This file contains 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 | |
submitted_jobs="" | |
submit() { | |
echo "> ${*}" > /tmp/job_XXX.log | |
eval "${*}" >> /tmp/job_XXX.log 2>&1 & | |
local id="${!}" | |
mv /tmp/job_XXX.log /tmp/job_${id}.log | |
if [[ -z "${submitted_jobs}" ]]; then | |
submitted_jobs="${id}" | |
else | |
submitted_jobs="${submitted_jobs} ${id}" | |
fi | |
} | |
await() { | |
local FAIL=0 | |
for job in ${submitted_jobs}; do | |
wait ${job} | |
if [[ $? != 0 ]]; then | |
FAIL=$((${FAIL} + 1)) | |
fi | |
cat /tmp/job_${job}.log | |
rm /tmp/job_${job}.log | |
done | |
submitted_jobs="" | |
if [[ ${FAIL} -gt 0 ]]; then | |
echo "Error detected, exiting" | |
exit 1 | |
fi | |
} | |
submit ssh some-server "echo do something" | |
submit ssh another-server "echo do something else" | |
await |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment