Re: RFC: A new module to help avoid running multiple instances of the same script (via cron, for example)by ikegami (Pope)
|on Dec 02, 2009 at 20:18 UTC||Need Help??|
The OS is better at controlling concurrency because it can do clean up for dead processes. Whether the file is locked or not could be used to indicate concurrency.
You had a bug where the lock can be defeated (accidentally or otherwise) by using a symlink to the script. It's fixed by realpath(rel2abs()) in my code.
die is overkill (punny!) for errors in removing the pid file. In fact, even a warning is sounds unnecessary to me.
An even better method for Windows would be to create a named mutex instead of creating a file. That way, it gets cleaned up automatically.
Alternatively, You could simplify the code a lot by simply locking the script.
I really dislike the name of the module. The module has nothing to do with cron and is not just useful for cron scripts, for starters. Then there's the problem that you don't want to prevent multiple runs. You want to prevent simultaneous runs.
A useful improvements would be to allow the caller to specify a lock file name if he's not happy with the default.