title: PHP/Cron Limit Number of Concurrently Running Scripts
body: <p>I have a script written in PHP that is kicked off by a cron job. It's a long running script, and often cron kicks off another instance before the last is finished. That's fine. I allow 3 instances of this script to run at any given time.</p>
  
  <p>I limit the script to 3 instances with this code at the top of the script:</p>
  
  <pre><code>exec('ps -A | grep nameofmyscript', $results);
  if (count($results) &gt; 4) {
      echo "Already Running\n"
      die(0);
  }
  </code></pre>
  
  <p>It works, but I'm looking for a better way. This approach backfired on me a few weeks back when I renamed the script, and forgot to change that line of code. It also fails when the script is named something similar to an already running process.</p>