Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Created November 15, 2022 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglascayers/b4d9391479765c3ce2bacb5e5fba9c48 to your computer and use it in GitHub Desktop.
Save douglascayers/b4d9391479765c3ce2bacb5e5fba9c48 to your computer and use it in GitHub Desktop.
Bash FOR loop with batched concurrency
#!/bin/bash
# https://unix.stackexchange.com/questions/103920/parallelize-a-bash-for-loop
NAMES=($(cat names.csv))
BATCH_SIZE=4
for NAME in ${NAMES[@]}; do
# Increment or reset our counter
((i = i % $BATCH_SIZE))
# The 'wait' pauses until all background commands are done
((i++ == 0)) && wait
# The '&' runs the command in background, non-blocking
some-cmd --some-args &
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment