Skip to content

Instantly share code, notes, and snippets.

@crookm
Created December 20, 2019 08:49
Show Gist options
  • Save crookm/a7a2a246935718d839738b4e4cbb9216 to your computer and use it in GitHub Desktop.
Save crookm/a7a2a246935718d839738b4e4cbb9216 to your computer and use it in GitHub Desktop.
Running parallel batch tasks in bash
#!/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
#!/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