加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从cli运行PHP脚本:php.ini memory_size没有考虑到

发布时间:2020-12-13 21:54:34 所属栏目:PHP教程 来源:网络整理
导读:我试图从命令行运行 PHP脚本(magento reindexer脚本).该脚本消耗大量内存,因此我收到以下错误:PHP致命错误:/home/karanta/www/karanta.fr/lib/Zend/Db中允许的内存大小为536870912字节耗尽(试图分配72个字节)第691行的/Adapter/Abstract.php 为了解决这个
我试图从命令行运行 PHP脚本(magento reindexer脚本).该脚本消耗大量内存,因此我收到以下错误:PHP致命错误:/home/karanta/www/karanta.fr/lib/Zend/Db中允许的内存大小为536870912字节耗尽(试图分配72个字节)第691行的/Adapter/Abstract.php

为了解决这个问题,我编辑了/etc/php5/cli/php.ini文件并设置了memory_limit = 2048M.

要检查配置,我运行一个包含phpinfo()的脚本;从cli我看到:memory_limit => 2048M => 2048M,因此似乎正确考虑了配置. ini_get(‘memory_limit的);也返回2048M.

但是当我重新运行reindex脚本时,我仍然得到PHP致命错误:允许的内存大小为536870912字节,因为memory_limit仍然是512M.该脚本无法完成,我没有想法如何扩充memory_limit以允许脚本完成.

编辑:我也尝试添加指令ini_set(“memory_limit”,-1);直接进入PHP脚本,它仍然挂起相同的PHP致命错误:允许的内存大小为536870912字节耗尽.

额外的信息:

服务器是运行Debian GNU / Linux 7.5和64GB RAM的ovh专用机器!

php -v返回:

PHP 5.6.12-1 (cli) Copyright (c) 1997-2015 The PHP Group Zend Engine
v2.6.0,Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev,Copyright (c) 1999-2015,by Zend Technologies

脚本执行期间free -k -h返回:

total        used        free      shared  buff/cache     available
Mem:            62G        1,8G         48G         84M          12G         60G
Swap:          1,0G          0B        1,0G

ps -aux返回:

karanta  25568  5.6  0.0 229484 41824 pts/1    S    07:54   0:04 php shell/indexer.php --reindex catalog_url

按照@ IgorGreg的推荐,我尝试使用Zend_Memory设置内存限制.我编写了这个PHP脚本,我从cli运行以替换shell / indexer.php脚本.

<?php
require_once 'app/Mage.php';
$app = Mage::app('admin');
umask(0);
$memoryManager = Zend_Memory::factory('none');
$memoryManager->setMemoryLimit(-1);
$process = Mage::getModel('index/process')->load(3);
$process->reindexAll();
?>

仍然得到同样的错误.

解决方法

CLI模式下的Magento解析.htaccess文件,如果找到内存限制或其他任何PHP设置,则应用它们(是的,认真……)

负责的代码在shell / abstract.php中:

/**
 * Parse .htaccess file and apply php settings to shell script
 *
 */
protected function _applyPhpVariables()
{
    $htaccess = $this->_getRootPath() . '.htaccess';
    if (file_exists($htaccess)) {
        // parse htaccess file
        $data = file_get_contents($htaccess);
        $matches = array();
        preg_match_all('#^s+?php_values+([a-z_]+)s+(.+)$#siUm',$data,$matches,PREG_SET_ORDER);
        if ($matches) {
            foreach ($matches as $match) {
                @ini_set($match[1],str_replace("r",'',$match[2]));
            }
        }
        preg_match_all('#^s+?php_flags+([a-z_]+)s+(.+)$#siUm',$match[2]));
            }
        }
    }
}

不要通过.htaccess设置内存限制和执行时间,使用php.ini或虚拟主机配置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读