2011-09-17 91 views
1

我想要一些php脚本在后台进程中启动。对于此,我有一种特殊的方法,在我的课无法使用php exec()(在magento中)执行PHP脚本

protected function _runSpider() 
{ 
    $php = exec('which php'); 
    $result = exec($php . ' ' . Mage::getBaseDir() . '/spider.php > ' . Mage::getBaseDir() . '/var/log/out.log 2>&1 &'); 
    Mage::log($result); 
} 

这应该执行这样的事情

/usr/bin/php /home/www/spider.php > home/www/var/log/out.log 2>&1 & 

但结果因为我觉得剧本没有执行,Magento的日志是空的,out.log文件是空的。

+0

您是否启用了安全模式? – EdoDodo

+0

另外检查你的主机是否禁用了“exec” - 你是否在列表中看到它:ini_get('disable_functions') – ddinchev

+0

exec('which php')很好,安全模式被禁用 –

回答

1

也许

protected function _runSpider() 
    { 
     $command = '$(which php) ' . Mage::getBaseDir() . '/spider.php > ' . Mage::getBaseDir() . '/var/log/out.log'); 
     $result = exec($command); 
     Mage::log($result); 
    } 

删除最后&将锁定脚本,而不是通过控制返回给解释的执行。而且,由于您将所有输出传送到一个文件,因此您可能不需要将输出传送到null

顺便说一句,您挂载的shell命令将不会返回任何数据,因为您将所有内容都传送到文件或/ dev/null。如果您需要回到$result var,请删除> filename部分代码,尽管exec将只有give you the last line of the result,您可能需要passthru()函数或添加一个$output var将每行存储到exec函数中的数组。