无法使用PHP exec执行PHP脚本

无法使用PHP exec执行PHP脚本

问题描述:

我正在尝试使用PHP exec调用来调用一个脚本,该脚本需要花费几秒钟的时间(带有第三方的Web服务).经过艰苦的努力,我将其简化为经典的hello world示例.调用脚本如下:

I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:

exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');

运行此命令时,输出execoutput.txt包含调用脚本页面的副本,而不是我期望的hello world.

When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.

为什么我不能使这个PHP脚本使用exec执行?请注意,当我将命令更改为类似ls -l的内容时,输出将是预期的目录清单.顺便说一句,万一这很重要,我将被调用的脚本更改为755 ...

Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...

更新-我将exec调用移到了调用脚本的末尾,至少现在我没有看到输出中执行了调用脚本.谢谢海报,我将尝试其中的一些想法.

Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.

帮助!

谢谢 史蒂夫

我也遇到了这个问题,事实证明这是php中的错误(#11430).解决方法是在php脚本中调用另一个php脚本时使用php-cli.因此,您仍然可以使用exec,而不是在浏览器中调用php-cli时使用php:

I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:

exec("php-cli  somescript.php");

这对我有用.