Getlock is an open source, simple and free command-line file locking application, which can lock one or more lockfiles, and then run a shell command.
getlock features several advanced options, allowing users to just lock files, to run a command when it can't lock the file, to kill off the current holder of the lockfile, etc. Below we've listed some examples of use, courtesy of the project's home page:
Lock Multiple files then run program, fail instantly if can't lock:
getlock /tmp/file1.lck /tmp/file2.lck "echo hello"
Try for 1 minute to lock a file, run program '/tmp/lockfail.sh' if can't get lock:
getlock -w -t 60 /tmp/file1.lck -F "/tmp/lockfail.sh" "echo got lock"
Kill owner of lockfile after waiting 30 secs:
getlock -w -g 30 -k /var/lock/file1.lck "echo got lock"
Just lock files, don't run a program:
getlock -w -N /var/lock/file1.lck /tmp/file2.lck
Run program even if can't get lock:
getlock -w -t 30 -X /tmp/file1.lck "/usr/sbin/program-that-must-run.sh"
Lock 3 files, do not write PID to the last of these:
getlock -w /tmp/lockfile1.lck /var/lock/lockfile2.lck -s /home/colum/MyWork.txt "vi /home/colum/MyWork.txt"
- This version adds the -v/-version command line flag and -n/-nohup command line flags.