[英]Can't execute PHP script using php exec() (in magento)
I want some php script to be launching in a background proccess. 我希望一些php脚本在后台启动。 For this i have a special method in my class 为此,我在课堂上有一个特殊的方法
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);
}
this should execute something like this 这应该执行这样的事情
/usr/bin/php /home/www/spider.php > home/www/var/log/out.log 2>&1 &
But in result as i think script is not executing, magento log is empty and out.log
file is empty. 但由于我认为脚本未执行,因此结果是magento日志为空,而out.log
文件为空。
Maybe 也许
protected function _runSpider()
{
$command = '$(which php) ' . Mage::getBaseDir() . '/spider.php > ' . Mage::getBaseDir() . '/var/log/out.log');
$result = exec($command);
Mage::log($result);
}
Removing the last & will lock execution of the script instead of passing control back to the interpreter. 删除最后一个&将锁定脚本的执行,而不是将控制权交还给解释器。 And since you're piping all of the output to a file, maybe you don't need to pipe the output to null
. 而且由于将所有输出管道传输到文件,因此也许不需要将输出管道传输为null
。
By the way, the shell command you mounted won't return any data as you're piping everything to a file or /dev/null. 顺便说一句,当您将所有内容通过管道传输到文件或/ dev / null时,安装的shell命令不会返回任何数据。 If you need it back to the $result
var, remove the > filename
part of the code, though exec will only give you the last line of the result and you may need the passthru() function or add an $output
var to store every line to an array on the exec
function. 如果需要返回到$result
变量,请删除代码中的> filename
部分,尽管exec仅会为您提供结果的最后一行,并且您可能需要passthru()函数或添加$output
变量来存储每个行到exec
函数上的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.