How to disable re-run the script?

Good afternoon.
It is necessary to prohibit the script to run more than 1 copy. Tell me, what's wrong?)
Early on freebsd and php 5.3 used the following code
if(file_exists('/tmp/.locker')){
 $f = fopen('/tmp/.locker', "w+");
 if(!flock($f, LOCK_EX|for lock_nb))$isLocked = true;

}else{
 $f = fopen('/tmp/.locker', "w+");
 if(!flock($f, LOCK_EX|for lock_nb)) $isLocked = true;
}

Accordingly, if isLocked === true, then we finish the script.

Now trying to run the same in php 5.6 and centos. The lock is not working. The first run creates a file, but the access to it get all instances. What am I wrong?
September 18th 19 at 23:46
3 answers
September 18th 19 at 23:52
var first_time = true;
if(first_time == true){
 first_time = false;
if(file_exists('/tmp/.locker')){
 $f = fopen('/tmp/.locker', "w+");
 if(!flock($f, LOCK_EX|for lock_nb))$isLocked = true;
}else{
 $f = fopen('/tmp/.locker', "w+");
 if(!flock($f, LOCK_EX|for lock_nb)) $isLocked = true;
}
}
and what is it? - kyra commented on September 18th 19 at 23:55
September 18th 19 at 23:48
Solution
Incidentally, I do not understand why you need the first if, if the code in both branches the same :)
Flock is generally not the right decision, I advise you to see the options
www.slideshare.net/fuenteovehuna/web-20-c?related=1
slides 34-35

PS Generally on 5.6 no change in this feature seems to be no, php.net/manual/ru/function.flock.php try to look at the option wouldblock
For a specific task you can certainly use memcache, but all problems will not solve it. The same problems arise as with the test based on the existence of the file. - kyra commented on September 18th 19 at 23:51
3 parameter always returns 0 for some reason... (and in the cases where the lock is and when not to).
Decided to check on the other servers... eventually on centOs 6.6 x32 refuses to work the lock. on x64 - working. - kyra commented on September 18th 19 at 23:54
September 18th 19 at 23:50
Why would you even want to open this file?
Use the locker as a flag, if the file exist, then an instance of the script running.

The logic is this:
1. Verify the file.
1.1 to Read it (get PID) to check whether process with that PID.
1.2 If you have one instance of a script is running, and we fail.
2. If not create the file
2.1 write in it the PID of the Current process
1.2 running script.
3. To delete the file.

And these flock - all the extra crap.
Considered this option... but there will have to use exec, otherwise the pid is not checked. - kyra commented on September 18th 19 at 23:53
: This option is extremely popular in bash scripting, and has proven its reliability and simplicity for many years. - kyra commented on September 18th 19 at 23:56

Find more questions by tags PHP