Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: reliable lockfiles? (lock)

by tye (Sage)
on Jun 20, 2012 at 16:05 UTC ( [id://977389] : note . print w/replies, xml ) Need Help??

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