Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Signals and END block

by bluto (Curate)
on Aug 04, 2004 at 16:00 UTC ( [id://380032] : note . print w/replies, xml ) Need Help??


in reply to Re^2: Signals and END block
in thread Signals and END block

Since I store the PID value of the process in a database, this PID might be re-used by the operating system when deleting the record at startup.

There is no way to handle 100% of the termination cases at the time of termination no matter what you do (e.g. your machine suddenly loses power and the UPS fails), so you must have code that detects stale records at some future point in time. There are several ways of doing this. The canonical way to verify a pid is to record it in a lock file that the process keeps locked while it is running. When you verify the pid, if you can lock the file, the process has obviously died. Another more generic way could be to write a separate "checkpoint" record that you remove in your END block. If you start up and the checkpoint record exists, you remove the stale PID record the that checkpoint record points to as well as the checkpoint record.