There are a number of non-perfect solutions to running only 1 instance of a process at a time. The common solutions are pid files, lock files, checking the process table - or some combination. The trouble with most of them is that they are not bullet proof (you can delete a locked file for instance). Dominus
published a presentation on File Locking Tips and Traps
back in 2003. One of the suggested solutions was to lock the file of the running process itself:
open SELF, "< $0" or die ...;
flock SELF, LOCK_EX | LOCK_NB or exit;
flock DATA, LOCK_EX | LOCK_NB or exit;
In the few instances I have needed this, one of these have worked. Recently I suggested a co-worker improve his code by using one of these techniques and he told me neither worked on AIX. The error in both cases is "unable to lock: A file descriptor does not refer to an open file."
Does anyone have any insight into why this is or have any suggestion on an alternative lightweight and nearly bullet proof way to accomplish the original objective?