Created
December 20, 2019 08:49
-
-
Save crookm/a7a2a246935718d839738b4e4cbb9216 to your computer and use it in GitHub Desktop.
Running parallel batch tasks in bash
This file contains hidden or 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
#!/bin/bash | |
task() { | |
sleep 1; echo "$1 - $(date)"; | |
} | |
N=2 | |
for item in {1..6}; do | |
((i=i%N)); ((i++==0)) && wait | |
task "$item" & | |
done | |
wait |
This file contains hidden or 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
#!/bin/bash | |
task() { | |
sleep 1; echo "$1 - $(date)"; | |
} | |
export -f task # so sem has access | |
N=2 # the size of each batch | |
for item in {1..6}; do | |
sem -j $N task "$item" | |
done | |
sem --wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment