PHP 脚本中的通用“杀死”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/952868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generic "Killed" error in PHP script
提问by Pro777
I am working on a CRON job that invokes a PHP script which does a lot of database work with loops.
我正在处理一个调用 PHP 脚本的 CRON 作业,该脚本使用循环执行大量数据库工作。
It executes properly when I limit the data set, but when I run it against the full data set, the script errors out with a message:
当我限制数据集时它会正确执行,但是当我针对完整数据集运行它时,脚本会出错并显示一条消息:
Killed
set_time_limit is (0) and memory_limit is (-1)
set_time_limit 是 (0) 和 memory_limit 是 (-1)
Here is the code section where it consistently 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 output looks like this:
输出如下所示:
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've never seen this generic Killederror message and I'm wondering what is causing it to be killed?
我从未见过这个通用Killed错误消息,我想知道是什么导致它被杀死?
回答by Steve Madsen
You might be triggering the Linux out-of-memory (OOM) killer. Check dmesgfor messages about it. It says which process was killed when this happens.
您可能正在触发 Linux 内存不足 (OOM) 杀手。检查dmesg有关它的消息。它说发生这种情况时哪个进程被杀死。
回答by Eric Leschinski
Simple way to reproduce this Killederror:
重现此Killed错误的简单方法:
I was able to reproduce this error on Ubuntu 12.10with PHP 5.3.10.
我能够Ubuntu 12.10用PHP 5.3.10.
Create a PHP script called m.phpand save it:
创建一个名为的 PHP 脚本m.php并保存:
<?php
function repeat(){
repeat();
}
repeat();
?>
Run it:
运行:
el@apollo:~/foo$ php m.php
Killed
The program takes 100% CPU for about 15 seconds then halts with the Killedmessage. Look at dmesg | grep phpand there are clues:
程序占用 100% CPU 大约 15 秒,然后停止并显示Killed消息。看了一下dmesg | grep php,有蛛丝马迹:
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.
所以在我的例子中,PHP 程序停止并打印“Killed”,因为它由于无限循环而耗尽了内存。
Solutions:
解决方案:
- Increase the amount of RAM available or amount of memory available to this PHP program.
- Break down the problem into smaller chunks that operate sequentially.
- Rewrite the program so it has smaller memory requirements or doesn't go so deep with recursion.
- 增加此 PHP 程序的可用 RAM 量或可用内存量。
- 将问题分解为按顺序运行的较小块。
- 重写程序,使其具有较小的内存需求,或者不会进行如此深入的递归。
回答by Boy
In my case on CloudLinux, PHP 7.1, it occurred when 2 processes were reading and writing to the same file without locks.
在我的 CloudLinux PHP 7.1 上,它发生在 2 个进程在没有锁定的情况下读取和写入同一个文件时。

