title: Dont run a cron php task until last one has finished
body: <p>I have a <code>php-cli</code> script that is run by <code>cron</code> every <code>5 minutes</code>. Because this interval is short, <code>multiple processes</code> are run at the same time. That's not what I want, since this script has to write inside a text file a numeric <code>id</code> that is incremented each time. It happens that <code>writers</code> are writing at the same time on this text file, and the value written is incorrect. </p>
  
  <p>I have tried to use php's <code>flock</code> function to block writing in the file, when another process is writing on it but it doesnt work.  </p>
  
  <pre class="lang-php prettyprint-override"><code>$fw = fopen($path, 'r+');
  if (flock($fw, LOCK_EX)) {
      ftruncate($fw, 0);
      fwrite($fw, $latestid);
      fflush($fw);
      flock($fw, LOCK_UN);
  }
  fclose($fw);
  </code></pre>
  
  <p>So I suppose that the solution to this is create a <code>bash</code> script that verifies if there is an instance of this <code>php script</code> that is running, if so it should wait until it finished. But I dont know how to do it, any ideas?</p>