Skip to content

Instantly share code, notes, and snippets.

@dirkgr
Created September 24, 2015 20:14
Show Gist options
  • Save dirkgr/1ab47c7d5864d9e9aabd to your computer and use it in GitHub Desktop.
Save dirkgr/1ab47c7d5864d9e9aabd to your computer and use it in GitHub Desktop.
How to lock in bash
#!/usr/bin/env bash
set -e
set -x
LOCKFILE=~/LOCK
if [ $1 -a $1 = "lockAcquired" ]; then
echo "I'm in."
sleep 10
else
echo "Acquiring lockfile $LOCKFILE ..."
flock $LOCKFILE $0 lockAcquired "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment