Home » Php » mysql – Generic "Killed" error in PHP script

mysql – Generic "Killed" error in PHP script

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am working on a CRON job that invokes a PHP script which does a lot of database work with loops.

It executes properly when I limit the data set, but when I run it against the full data set, the script errors out with a message:

Killed

set_time_limit is (0) and memory_limit is (-1)

Here is the code section where it consistently dies:

echo "I'm in _getMemberDemographicAttrs\n";
if (! empty ( $member_id )) {
    $query .= ' AND member_id = ' . $member_id;
}

$result = mysql_query ( $query, $this->_db );
if ($result) {
    while ( $rule = mysql_fetch_assoc ( $result ) ) {
        $rules [] = $rule;
    }
    if (! empty ( $rules )) {
        mysql_free_result ( $result );
        echo "I'm leaving _getMemberDemographicAttrs\n";
        return $rules;
    }
}

The output looks like this:

I'm in _getMemberDemographicAttrs<br/>
I'm leaving _getMemberDemographicAttrs<br/>
I'm in _getMemberDemographicAttrs<br/>
I'm leaving _getMemberDemographicAttrs<br/>
I'm in _getMemberDemographicAttrs<br/>
Killed

I’ve never seen this generic Killed error message and I’m wondering what is causing it to be killed?

How to&Answers:

You might be triggering the Linux out-of-memory (OOM) killer. Check dmesg for messages about it. It says which process was killed when this happens.

Answer:

Simple way to reproduce this Killed error:

I was able to reproduce this error on Ubuntu 12.10 with PHP 5.3.10.

Create a PHP script called m.php and save it:

<?php
    function repeat(){
       repeat();
    }
    repeat();
?>

Run it:

[email protected]:~/foo$ php m.php
Killed

The program takes 100% CPU for about 15 seconds then halts with the Killed message. Look at dmesg | grep php and there are clues:

[email protected]:~/foo$ dmesg | grep php
[2387779.707894] Out of memory: Kill process 2114 (php) score 868 or 
sacrifice child

So in my case, the PHP program halted and printed “Killed” because it ran out of memory due to an infinite loop.

Solutions:

  1. Increase the amount of RAM available or amount of memory available to this PHP program.
  2. Break down the problem into smaller chunks that operate sequentially.
  3. Rewrite the program so it has smaller memory requirements or doesn’t go so deep with recursion.

Answer:

In my case on CloudLinux, PHP 7.1, it occurred when 2 processes were reading and writing to the same file without locks.