Skip to content

Instantly share code, notes, and snippets.

@ryul99
Created August 9, 2024 13:57
Show Gist options
  • Save ryul99/90cab470fdade2b62481d9685da6a020 to your computer and use it in GitHub Desktop.
Save ryul99/90cab470fdade2b62481d9685da6a020 to your computer and use it in GitHub Desktop.
Restrict the maximum number of jobs running in the background while running in parallel.
# This code CANNOT be run as a script.
# You should add this code to your shell rc file and use it as a shell function.
# This is because the `jobs` command can only get child processes.
#
# example: `for i in $(seq 1 24); do run-parallel sleep 6; done; wait`
function run-parallel() {
MAX_JOBS=${MAX_JOBS:-6}
while [ $(jobs -r | wc -l) -ge $MAX_JOBS ]; do
sleep 1
done
$@ &
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment