Home‎ > ‎


Getlock is a simple command-line file locking program. In normal use it locks one or more lockfiles, and then runs a shell command. However, getlock has many options, allowing it to just lock the files, or to kill off the current holder of the lockfile, or to run a command when it can't lock the file, etc, etc.
I use this mostly to stop programs running from cron from 'crashing into each other'. I used to use the 'lockfile' program, but as this worked by creating file, and checking if the existed, lockfiles could be left behind if the system shut down or the process was killed. This would mean that the program wouldn't run until an 'orphaned' lockfile was deleted. This was annoying, so I implemented getlock, which uses fcntl locks that free if the process ends for any reason.

Examples of use:

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"

And much else besides!