title: How do I detect that a PHP CLI script is in a Hung state
body: <p>I am using supervisor (http://supervisord.org/) to daemonize a fairly standard PHP script.  The script is structured something like:</p>
  
  <pre><code>while (1) {
  //  Do a SQL select
  //  for any matching rows, do something
  //  if I have been running for longer than 60 mins, exit
  }
  </code></pre>
  
  <p>Today, this script (which has been fairly stable for some time now), hung.  It did not crash (ie issue SIGHUP or SIGTERM signals) which would have alerted supervisord to restart the process.  It did not encounter any errors in its processing, which would have either been caught by the script, or at least have triggered a fatal error and exited.  Instead of these "catchable" scenarios, it just sat there.  We do have a cron job setup to run every hour to restart the script through the supervisorctl hook, because it seems to be generally accepted that PHP scripts are leaky in terms of memory and would do well to be restarted if running long.  The script resumed operations normally after that reboot.</p>
  
  <p>My question:  how can I detect that this script has hung?  I can't even begin to diagnose or troubleshoot this problem of why it has hung, if I am not somehow alerted to that state.  I am looking for either a software solution to this, or some approach that I can take to author a solution myself ( in either PHP, Python, perl or shell).</p>
  
  <p>The script is written in PHP 5.2.6, and runs on a uptodate RHEL 5 server. </p>
  
  <p>Please let me know if I can share any additional information if it will help with a more awesome solution.</p>
  
  <p>Thank you!</p>
  
  <p>Shaheeb R. </p>