http://www.perlmonks.org?node_id=977389


in reply to reliable lockfiles?

Create the lock file if it doesn't exist. Then lock it, exclusively (such as via flock). No matter how your process dies, the lock will be released when the process is no longer running. Trying to lock the lock file will fail only if another process is already running. You don't even have to worry about a PID getting re-used. Don't have code that deletes the lock file. For monitoring convenience, after locking of the file succeeds, write your PID to the file.

This also has the major advantage of preventing a classic race condition where you end up with two instances running.

- tye