Skip to content

Instantly share code, notes, and snippets.

@nrclark
Last active July 2, 2017 01:44
Show Gist options
  • Save nrclark/7611d3a8a20d9b493bdc7193c9565d85 to your computer and use it in GitHub Desktop.
Save nrclark/7611d3a8a20d9b493bdc7193c9565d85 to your computer and use it in GitHub Desktop.
Shell script for running commands in parallel
#!/bin/sh
#
# Quick-and-dirty parallelizer, by Nicholas Clark
#
# Uses shell job-control to launch each argument as a subprocess.
# Waits until all subprocesses have finished. If all subprocesses
# returned 0, the result of the script is 0. Otherwise, the result
# will be the error-code of the first failed command in the argument
# list.
#
# All jobs are started at more-or-less the same time, so the number
# of subprocesses should probably be kept below 20 or so.
PIDS=""
RESULT=0
for _ in $(seq $#); do
eval "$1" &
PIDS="$PIDS $!"
shift
done
for pid in $PIDS; do
wait "$pid"
_result=$?
if [ "$_result" != "0" ]; then
RESULT="$_result"
break
fi
done
wait
exit $RESULT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment