Last active
July 28, 2023 00:29
-
-
Save therightstuff/f4cc70db21d8e21d7277a414dbefa0a6 to your computer and use it in GitHub Desktop.
Sample bash script for managing multiple parallel processes
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 | |
# inspired by https://stackoverflow.com/a/29535256/2860309 | |
pids="" | |
failures=0 | |
function my_process() { | |
seconds_to_sleep=$1 | |
exit_code=$2 | |
sleep "$seconds_to_sleep" | |
return "$exit_code" | |
} | |
(my_process 1 0) & | |
pid=$! | |
pids+=" ${pid}" | |
echo "${pid}: 1 second to success" | |
(my_process 1 1) & | |
pid=$! | |
pids+=" ${pid}" | |
echo "${pid}: 1 second to failure" | |
(my_process 2 0) & | |
pid=$! | |
pids+=" ${pid}" | |
echo "${pid}: 2 seconds to success" | |
(my_process 2 1) & | |
pid=$! | |
pids+=" ${pid}" | |
echo "${pid}: 2 seconds to failure" | |
echo "..." | |
for pid in $pids; do | |
if wait "$pid"; then | |
echo "Process $pid succeeded" | |
else | |
echo "Process $pid failed" | |
failures=$((failures+1)) | |
fi | |
done | |
echo | |
echo "${failures} failures detected" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment