Generic "Killed" error in PHP script

30,625

Solution 1

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

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.
Share:
30,625
Pro777
Author by

Pro777

Updated on July 09, 2022

Comments

  • Pro777
    Pro777 almost 2 years

    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?

  • Darth Egregious
    Darth Egregious over 11 years
    Thanks for this. I found that Linux was killing the process. I resolved it by reducing the memory limit for PHP in the script, which allowed PHP to manage its memory differently and avoid the crash.
  • marlar
    marlar about 11 years
    Thanks. Just what I needed.
  • Jacques
    Jacques over 6 years
    For me, the problem was resolved by upgrading from PHP 5.6 to PHP 7. Obviously it depends on many factors but PHP 7 works more efficient so it is worth checking out.
  • Adam
    Adam about 6 years
    For me, I'm running PHP7 and I've got killed, even though I've 3GB free ram! and PHP memory limit was set to -1, I'm trying to @Fuser97381 solution.