Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to find source of PHP script termination

I have a PHP script that grabs a chunk of data from a database, processes it, and then looks to see if there is more data. This processes runs indefinitely and I run several of these at a time on a single server.

It looks something like:

<?php
    while($shouldStillRun)
    {
       // do stuff
    }
    logThatWeExitedLoop();
?>

The problem is, after some time, something causes the process to stop running and I haven't been able to debug it and determine the cause.

Here is what I'm using to get information so far:

  • error_log - Logging all errors, but no errors are shown in the error log.
  • register_shutdown_function - Registered a custom shutdown function. This does get called so I know the process isn't being killed by the server, it's being allowed to finish. (or at least I assume that is the case with this being called?)
  • debug_backtrace - Logged a debug_backtrace() in my custom shutdown function. This shows only one call and it's my custom shutdown function.
  • Log if reaches the end of script - Outside of the loop, I have a function that logs that the script exited the loop (and therefore would be reaching the end of the source file normally). When the script dies randomly, it's not logging this, so whatever kills it, kills it while it's in the middle of processing.

What other debugging methods would you suggest for finding the culprit?

Note: I should add that this is not an issue with max_execution_time, which is disabled for these scripts. The time before being killed is inconsistent. It could run for 10 seconds or 12 hours before it dies.


Update/Solution: Thank you all for your suggestions. By logging the output, I discovered that when a MySql query failed, the script was set to die(). D'oh. Updated it to log the mysql errors and then terminate. Got it working now like a charm!

like image 822
Nate Weiner Avatar asked Nov 24 '10 23:11

Nate Weiner


1 Answers

I'd log memory usage of your script. Maybe it acquires too much memory, hits memory limit and dies?

like image 87
Kamil Szot Avatar answered Oct 04 '22 01:10

Kamil Szot