Skip to content

Instantly share code, notes, and snippets.

@3manuek
Last active June 1, 2024 10:57
Show Gist options
  • Save 3manuek/453e7dff8234da19057ad7c59e69eb3e to your computer and use it in GitHub Desktop.
Save 3manuek/453e7dff8234da19057ad7c59e69eb3e to your computer and use it in GitHub Desktop.
Functional implementation of Bash workers
#!/bin/bash
set -e -o pipefail
setWaiter(){
export maxWorkers=${1}
}
waiter(){
while test $(jobs -p | wc -l) -ge $maxWorkers ; do wait -n ; done
}
waitall(){
wait -n
}
# Usage
#
#
# . workers.sh
# setWaiter 3
#
# waiter ( ls ; for ... ; otherCommand ; ) &
#
# wait -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment