Skip to content

Instantly share code, notes, and snippets.

@manuq
Created September 15, 2015 17:46
Show Gist options
  • Save manuq/a4b6790223906a9ae3c6 to your computer and use it in GitHub Desktop.
Save manuq/a4b6790223906a9ae3c6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# bash multi flock
#
# allow 3 instances of a bash function running at the same time
run_instance() {
(
flock -n 99 || return 1
echo "starting instance $@..."
# do something here
sleep 10
echo "done"
) 99>/tmp/test-${@}.lock
}
run_instance 1 || run_instance 2 || run_instance 3 || echo "BLOCKED!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment