PHP script is killed without explanation

隐身守侯 提交于 2019-12-20 01:48:05

问题


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?


回答1:


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?




回答2:


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.



回答3:


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




回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/20520194/php-script-is-killed-without-explanation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!