Hello All,

I am confused with the flock function and its usage. I have jobs which are
stored in a database, these jobs are run by a series of job_runners scripts
but sometimes the job_runners stop ( server or php crash-down). So i put a
job_controller in crontab to check  regularly if the  runners run. But after
a while I have a bunch of job_controller running, so to avoid that I tried
to use flock.

I try to put this in the job_controller:

$wouldblock=1;
$f=fopen("controller.lock", "r");
flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die("Error! cant lock!");

hoping that as long as the first job_controller run or don't close the file
handle, a second job_controller won't be able to lock the
controller.lockfile and die, but it didn't work.

I also try this:

$wouldblock=1;
$f=fopen("controller.php", "r");
flock($f, LOCK_EX+LOCK_NB, $wouldblock) or die("Error! cant lock!");

hoping the first job_controller will lock it-self, but it didn't work.

I also thought of writing in the lock file the PID of the first
job_controller and then compare it and if it doesn't match then die, but my
main concern is , if the server crash down the "surviving" lock file will
prevent any job_controller to start.

So how could prevent multiple instance of the same script? Is flock the best
way?

Thanks in advance for your time and help

cheers

yvan

Reply via email to