5

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Robin93K
  • 198
  • 2
  • 3
  • 15
  • 2
    putty session and all its running process is killed when the shell ends. use `nohup` before command – bansi Dec 11 '13 at 13:20
  • Just terminology maybe, but for better understanding: putty is just the thing that lets you open an `SSH` session to your server. It is really unlikely that putty is killing your process. You are in a session, and somthing killes your process. And you see it in the session (putty makes the session). – Nanne Dec 11 '13 at 13:20
  • Seeing all the nohup answers: your session is still running right? you can just do an 'ls' after you see 'killed' ? If so, those answers will probably not help you... – Nanne Dec 11 '13 at 13:22
  • @bansi, funnily I really tried it, even so that I didn't closed the session, without success... – Robin93K Dec 11 '13 at 13:24
  • @Nanne Yeah I know that it only connects me with our server, but there is nothing on the server, as soon as I know, which could kill my process... – Robin93K Dec 11 '13 at 13:25
  • check if your script is hitting maximum execution time php.net/manual/en/function.set-time-limit.php – bansi Dec 11 '13 at 13:25
  • @bansi : that would not send a 'killed' but an error. – Nanne Dec 11 '13 at 13:26

5 Answers5

9

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?

Community
  • 1
  • 1
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • Is there any function for PHP to check the current Swap Usage or a bash cmd to check live how mush Swap is used ? – Robin93K Dec 11 '13 at 13:50
  • sure. Log in separately while running (or run your command in the background, and use commands like `top` and `free` to check on your server state, though complete run-trought of this might be somewhat beyond scope of this question. Try to look up the manpage. – Nanne Dec 11 '13 at 13:53
  • where can I see the Swap usage? I only see the memory usage. – Robin93K Dec 11 '13 at 14:12
  • sry... forget that question I was kinda blind xD – Robin93K Dec 11 '13 at 14:30
  • Someone should add to this answer, that it is pretty hard to figure out when set_time_limit() is set, since it doesn't show any "Killed" in php terminal or so, which is different from memory_limit behaviour – shakaran Jul 29 '16 at 03:33
  • I'm not sure what you mean to be honest. If you mean that the *php* limits for time and memory have different behaviour, then I don't understand it. As I tried to type above, none of the php-limits will print 'killed' as far as I know, so that behaviour is the same? – Nanne Jul 29 '16 at 07:28
4

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.
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
2

Killed is what bash says when a process exits after a SIGKILL, it's not related to putty.

Terminated is what bash says when a process exits after a a SIGTERM.

You are not running into PHP limits, you may be running into a different problem, see:

Return code when OOM killer kills a process

Community
  • 1
  • 1
mr.spuratic
  • 9,767
  • 3
  • 34
  • 24
  • 1
    I'd say you _don't_ run in to php limits, as you'd see an error instead of a sigkill! – Nanne Dec 11 '13 at 13:25
  • @Nanne facepalm. I really should read my own answers more carefully, fixed thx! – mr.spuratic Dec 11 '13 at 13:29
  • @mr-spuratic I'D like to check that, is there any PHP-version of WIFSIGNALED() & WTERMSIG()? or where shallI let them run? – Robin93K Dec 11 '13 at 13:54
  • Those are only of use in the *parent* process to see what happened to a child. That said, see PHP's [`pcntl`](http://www.php.net/manual/en/ref.pcntl.php) functions. [`memory_get_peak_usage()`](http://php.net/manual/en/function.memory-get-peak-usage.php) might be useful too. I don't think PHP has [enough signal support](http://stackoverflow.com/questions/8400530/how-can-i-tell-in-linux-which-process-sent-my-process-signal) to find details of a received signal. – mr.spuratic Dec 12 '13 at 10:26
-2

If you are already taking care of php.ini settings related with script memory and timeout then may be its linux ssh connection which terminating in active session or some thing like that.

You can use 'nohup' linux command run a command immune to hangups

shell> nohup php -f 'scriptname'.php

Edit:- You can close your session by adding '&' at end of command:-

shell> nohup php -f 'scriptname'.php &> /dev/null &

'&' operater at end of any comand in linux move that command in background

Prabhat Kumar
  • 310
  • 1
  • 8
  • Assuming the session still runs (you do see 'killed' after all), I see no reason why there should be a hangup? – Nanne Dec 11 '13 at 13:22
-2

http://en.wikipedia.org/wiki/Nohup

Try using nohup before your command.

nohup catches the hangup signal while the ampersand doesn't (except the shell is confgured that way or doesn't send SIGHUP at all).

Normally, when running a command using & and exiting the shell afterwards, the shell will terminate the sub-command with the hangup signal (kill -SIGHUP ). This can be prevented using nohup, as it catches the signal and ignores it so that it never reaches the actual application.

In case you're using bash, you can use the command shopt | grep hupon to find out whether your shell sends SIGHUP to its child processes or not. If it is off, processes won't be terminated, as it seems to be the case for you.

There are cases where nohup does not work, for example when the process you start reconnects the NOHUP signal.

nohup php -f 'yourscript'.php
Acelasi Eu
  • 914
  • 2
  • 9
  • 30