Generic "Killed" error in PHP script

PhpMysqlCron

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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:

el@apollo:~/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:

el@apollo:~/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.

Solution 3 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPro777View Question on Stackoverflow
Solution 1 - PhpSteve MadsenView Answer on Stackoverflow
Solution 2 - PhpEric LeschinskiView Answer on Stackoverflow
Solution 3 - PhpBoyView Answer on Stackoverflow