Created
May 28, 2013 10:22
-
-
Save larsar/5661829 to your computer and use it in GitHub Desktop.
Example shell script for forking processes in parallell and then waiting for them to finish.
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
#!/bin/bash | |
pids=() | |
# Snooooze for two seconds | |
function do_stuff { | |
sleep 2 | |
} | |
# Starts 10 processes | |
for i in `seq 10`; do # start 30 jobs in parallel | |
do_stuff & | |
pids+=($!) | |
done | |
# When this loop ends, all child processes are finished. | |
for pid in ${pids[@]}; do | |
wait $pid | |
echo "Finished process with pid: $pid" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment