Skip to content

Instantly share code, notes, and snippets.

@zurfyx
Last active August 15, 2018 15:35
Show Gist options
  • Save zurfyx/334dd3764e550e3057bb713d7d0f7376 to your computer and use it in GitHub Desktop.
Save zurfyx/334dd3764e550e3057bb713d7d0f7376 to your computer and use it in GitHub Desktop.
Run commands parallelly
#!/bin/bash
# https://stackoverflow.com/a/19618159
# https://stackoverflow.com/a/51861820
#
# Example file contents:
# touch /tmp/a.txt
# touch /tmp/b.txt
if [ "$#" -eq 0 ]; then
echo "$0 <file> [max-procs=0]"
exit 1
fi
FILE=${1}
MAX_PROCS=${2:-0}
cat $FILE | while read line; do printf "%q\n" "$line"; done | xargs --max-procs=$MAX_PROCS -I CMD bash -c CMD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment