Generic "Killed" error in PHP script Generic "Killed" error in PHP script php php

Generic "Killed" error in PHP script


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.


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.phpKilled

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.


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