Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP script is killed without explanation

Tags:

bash

php

putty

I'm starting my php script in the following way:

bash  
cd 'path'   
php -f 'scriptname'.php

There is no output while the php script is running.

After a time, the php script responds with:

Killed 

My idea is that it reached the memory_limit: ini_set('memory_limit', '40960M');

Increasing the memory limit seemed to solve the problem, but it only increased the edge.

What exactly does that Killed phrase mean?

like image 281
Robin93K Avatar asked Feb 14 '23 10:02

Robin93K


2 Answers

Your process is killed. There could be a multitude of reasons, but it's easy to discard some of the more obvious.

  • php limits: if you run into a php limit, you'll get an error in the logfile, and probably on the commandline as well. This normally does not print 'killed'
  • the session-is-ended-issues: if you still have your session, then your session is obvioiusly not ended, so disregard all the nohup and & stuff

If your server is starved for resources (no memory, no swap), the kernel might kill your process. This is probably what's happening.

In anycase: your process is getting send a signal that it should stop. Normally only a couple of 'things' can do this

  • your account (e.g. you kill the process)
  • an admin user (e.g. root)
  • the kernel when it is really needing your memory for itself.
  • maybe some automated process, for instance, if you live on a shared server and you take up more then your share of resources.

references: Who "Killed" my process and why?

like image 200
Nanne Avatar answered Feb 17 '23 11:02

Nanne


You could be running out of memory in the PHP script. Here is how to reproduce that error:

I'm doing this example on Ubuntu 12.10 with PHP 5.3.10:

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

Solutions:

  1. Increase the amount of RAM available.
  2. Break down the problem set into smaller chunks that operate sequentially.
  3. Rewrite the program so it has a much smaller memory requirements.
like image 45
Eric Leschinski Avatar answered Feb 17 '23 11:02

Eric Leschinski