Last active
January 4, 2017 14:51
-
-
Save shokoe/4b8a3fa98587c6e4eb4e46ebd800526c to your computer and use it in GitHub Desktop.
Simple bash lock file example using lockfile-progs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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