Skip to content

Instantly share code, notes, and snippets.

@larsar
Created May 28, 2013 10:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsar/5661829 to your computer and use it in GitHub Desktop.
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.
#!/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