Skip to content

Instantly share code, notes, and snippets.

@shokoe
Last active January 4, 2017 14:51
Show Gist options
  • Save shokoe/4b8a3fa98587c6e4eb4e46ebd800526c to your computer and use it in GitHub Desktop.
Save shokoe/4b8a3fa98587c6e4eb4e46ebd800526c to your computer and use it in GitHub Desktop.
Simple bash lock file example using lockfile-progs.
#!/bin/bash
# Requires: apt-get install lockfile-progs
# Test: /root/locktest.sh& sleep 2; /root/locktest.sh
lock_file='/var/run/locker'
# 0 for immidiate fail, see 'man lockfile-progs' for details
lock_retry=0
lock(){
lockfile-create -r $lock_retry -p $lock_file && return 0
echo "ERROR: Can't get lock"
exit $?
}
unlock(){ lockfile-remove $lock_file; }
lock
sleep 10
unlock
echo done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment