Common "Killed" error in PHP script

I am working on a CRON job that calls a PHP script that works a lot with loops.

It runs correctly when I limit the data set, but when I run it against the full data set, script errors are output with the message:

Killed 

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

Here is the section of code where it sequentially 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 result is as follows:

 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 have never seen this general Killed error message, and I wonder what kills it?

+46
php mysql cron
Jun 04 '09 at 20:20
source share
3 answers

You might be running the Linux killer from memory (OOM). Check dmesg for reports of this. It says which process was killed when that happens.

+64
Jun 04 '09 at 20:22
source share
β€” -

A simple way to reproduce this Killed error:

I was able to reproduce this error on Ubuntu 12.10 using 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 up 100% of the processor for 15 seconds, then stops with the message Killed . Look dmesg | grep php dmesg | grep php and there are tips:

 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 stopped and printed "Killed" because it ran out of memory due to an infinite loop.

Solutions:

  • Increase the amount of available memory or the amount of memory available for this PHP program.
  • Divide the problem into smaller pieces that work in sequence.
  • Rewrite the program so that it has less memory requirements or is not so deep with recursion.

How not to get this problem again

If the code you wrote causes this error and you feel stuck and don’t understand why it is doing this, you need to revisit the main core behavior of PHP structures, loops and recursion, as well as how the memory allocated to satisfy these designs: http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/

+14
Mar 13 '14 at 17:23
source share

In my case in CloudLinux, PHP 7.1, this happened when 2 processes were reading and writing to the same file without locks.

0
Dec 05 '18 at 23:59
source share



All Articles