Created
August 9, 2024 13:57
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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