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


in reply to reliable lockfiles?

What I'd really like to achieve would be a bulletproof lockfile that can withstand this kind of situation. I remembered that File::Temp has an nice UNLINK property that forces the file to be deleted when the process exits. I'm not sure if this happens when the process is killed, however. But that's the sort of functionality that I'm looking for.

There are two approaches for deleting temp files. The first is an END block that deletes the file (but that doesn't help in the case of power outage), and the second is to call unlink while the file is still open. on UNIX systems that gives hides the file from all processes, but it remains on disc until no more file handles to it are open. Of course that's no good for locking either.

Another approach to the lock files is to put them into a location that the operating system clears out at boot time (/var/lock/ on Debian systems), or to put them on RAM discs in the first place, whose contents automatically disappear at power down/reboot.

I realize that all of that isn't exactly what you're looking for, but maybe it still gives you some ideas.