User Tools

Site Tools


programming:c:lockf

What Is It?

lockf is a piece of software that comes with FreeBSD (as well as other BSDs, I would assume). It's a simple C program that allows you to create a lock file with an exclusive lock and start a program running. Here is the man page. It's a simple program with a simple implementation that is very useful. This was written by John D. Polstra, copyright 1997, and I have taken the source and modified it so that it will work on linux systems.

Why Use It?

This is very helpful when running a cron job that might run long enough that it would “overlap” with the next cron run. This will cause the overlapping instance to exit, alternatively after -t <seconds> timeout.

Download

Download the source package: lockf_linux.tar.gz

Install

After downloading the above tar.gz, run the following as root in the directory you downloaded it to:

# tar -xzf lockf_linux.tar.gz
# cd lockf
# make && make install

The above will install the binary in /usr/bin and the man page in /usr/share/man/man1. If you want something different, just modify the paths in the Makefile.

Usage

$ lockf --help
usage: lockf [-ks] [-t seconds] file command [arguments]

A typical call would be:

$ lockf -t 30 /tmp/mybinary.lock mybinary --arg=blah --arg2=blah2

This would create a lockfile, /tmp/mybinary.lock, that would cause any more lockf calls with that file to fail. The -t 30 means that if lockf cannot obtain a lock after 30 seconds, it will exit.

programming/c/lockf.txt · Last modified: 2008/04/30 18:22 by crustymonkey