php script will only run one at a time 
<p>I have a script that I want to make sure only one is running, I am doing:</p>

<pre><code>$fp = fopen("lock.txt", "w");
if (flock($fp, LOCK_EX|LOCK_NB)) { // do an exclusive lock
   //Processing code

   flock($fp, LOCK_UN);
}else{
   echo "Could not lock file!";
}

fclose($fp);
</code></pre>

<p>The problem is when I start the second script, it just stays there waiting. If I then stop the first script, the second script will then print "Could Not lock this file". Why doesn't the second script just stop immediately and report that message?</p>

<p>If it ran the second script, it would know that the file is locked and should exit. When I watch my processes I can see the second script sat there ... it's just wating.</p>

<p>Thanks.</p>

<p>EDIT: I have just tried a quick and dirty database lock i.e set a running field to -1 and check for it when the script opens. But that doesn't work. I have also tried using sockets as described: <a href="http://stackoverflow.com/questions/1861321/how-to-prevent-multiples-instances-of-a-script">here</a> . It seems like the second file won't even run ... Should I even be worried?</p>
