Skip to content

Instantly share code, notes, and snippets.

@fipar
Created November 20, 2014 13:20
Show Gist options
  • Select an option

  • Save fipar/89c824586e61ff03e5cd to your computer and use it in GitHub Desktop.

Select an option

Save fipar/89c824586e61ff03e5cd to your computer and use it in GitHub Desktop.
worker pool in bash
#!/bin/bash
THREADS=3
worker()
{
echo "thread $1 starting"
sleep 3
rm -f /tmp/lock.$1
echo "thread $1 finished"
}
for i in $(seq 10); do
[ $(ls /tmp/lock.* 2>/dev/null|wc -l) -ge $THREADS ] && {
echo "$THREADS threads already running, waiting for one to finish">&2
sleep 1
continue
}
lock=$RANDOM
worker $lock &
touch /tmp/lock.$lock
echo "started thread $lock"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment