I need to create a lock file that can not be deleted by other scripts or processes. I tried this:
$f = fopen($pidFile, 'w');
fwrite($f, getmypid());
flock($f, LOCK_EX);
but any other process started from the current user can delete the $f
file even if the file handle is still opened by the running script. How to solve this problem and prevent others (i.e. non-PHP processes too) from deleting the file? And the lock to be released automatically when the process exits? All similar questions ends with an RTM flock(), but none of them answer how to lock a file against external processes.
OS is Linux 2.6.32-431.el6.x86_64
flock
is "advisory", not strict. Use additional check before doing anything with file in your scripts. This will work if it's about your own PHP scripts. If it's about external processes - thenflock
can not save the case.