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

fko*_*ler 3 php command-line-interface magento memory-limit reindex

我试图从命令行运行PHP脚本(magento reindexer脚本).该脚本消耗大量内存,因此我收到以下错误:PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 72 bytes) in /home/karanta/www/karanta.fr/lib/Zend/Db/Adapter/Abstract.php on line 691

要解决此问题,我编辑了/etc/php5/cli/php.ini文件并进行了设置memory_limit = 2048M.

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

但是当我重新运行reindex脚本时,我仍然PHP Fatal error: Allowed memory size of 536870912 bytes exhausted感觉到memory_limit仍然是512M.该脚本无法完成,我没有想法如何扩充memory_limit以允许脚本完成.

编辑:我也尝试将指令ini_set("memory_limit", -1);直接添加到PHP脚本中,它仍然挂起相同PHP Fatal error: Allowed memory size of 536870912 bytes exhausted.

额外的信息:

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

php -v 收益:

PHP 5.6.12-1(cli)版权所有(c)1997-2015 PHP Group Zend Engine v2.6.0,Copyright(c)1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev,Copyright(c)1999-2015 ,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
Run Code Online (Sandbox Code Playgroud)

ps -aux 收益:

karanta  25568  5.6  0.0 229484 41824 pts/1    S    07:54   0:04 php shell/indexer.php --reindex catalog_url
Run Code Online (Sandbox Code Playgroud)

按照@ 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();
?>
Run Code Online (Sandbox Code Playgroud)

仍然得到同样的错误.

Fab*_*ler 7

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_value\s+([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_flag\s+([a-z_]+)\s+(.+)$#siUm', $data, $matches, PREG_SET_ORDER);
        if ($matches) {
            foreach ($matches as $match) {
                @ini_set($match[1], str_replace("\r", '', $match[2]));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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