无法使用PHP exec执行PHP脚本

时间:2021-05-27 01:21:44

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:

我试图使用PHP exec调用调用一个需要几秒钟(与第三方的Web服务)的脚本。经过多次努力,我把它减少到了经典的hello world例子。调用脚本如下所示:

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

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

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

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执行此PHP脚本?请注意,当我将命令更改为ls -l之类的命令时,输出是按预期方式列出的目录。顺便说一句,如果重要的话,我做了chmod被调用的脚本到755 ...

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.

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

Help!

Thanks Steve

6 个解决方案

#1


28  

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:

我也有这个问题,事实证明这是php中的一个错误(#11430)。修复是在php脚本中调用另一个php脚本时使用php-cli。所以你仍然可以使用exec,而不是在浏览器中调用它时使用php使用php-cli:

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

This worked for me.

这对我有用。

#2


16  

What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.

exec正在做的是采取最右边的命令并将其附加到目的地。如果你的PHP脚本中有shebang行,你不需要包含php解释器的二进制指令。

if you just want the script's output, try:

如果您只想要脚本的输出,请尝试:

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

however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:

但是,如果您不希望错误出现在文件中,则应在输出到文件之前重定向STDERR。像这样:

exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')

the above should only output the "Hello World" to the execoutput.

以上应该只向execoutput输出“Hello World”。

Edit:

Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:

有趣的是你得到这种行为。你声明命令“ls”有效。尝试为此创建别名并将其转发到如下文件:

alias pexec='php /home/quote2bi/tmp/helloworld.php'

别名pexec ='php /home/quote2bi/tmp/helloworld.php'

then

exec('pexec > /tmp/execoutput.txt 2>&1 &')

it seems to be a problem with the way exec handles input as opposed to the shell itself.

它似乎是exec处理输入的方式的问题,而不是shell本身。

-John

#3


8  

The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.

问题出在PHP本身,它将所有内容视为脚本中的$ argv。它不会将输出重定向到文件ou / dev / null。

I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:

前段时间我遇到了同样的问题。我所做的是在/ opt / php-bin中创建一个runscript.php,然后在这个脚本中运行它应该运行的内容。像这样的东西:

$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd    = "{$script} {$params} > /dev/null &";

$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);

exit((int)$return);

And then you call it using:

然后你用它来调用它:

exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')

It´s the only way I managed to get this working.

这是我设法让这个工作的唯一方法。

#4


1  

if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like

如果你只是简单地运行一个php脚本,一种可能的方法来执行整个代码就是使用include()来运行php文件并输出任何结果。您不能将输出定向到文本文件,但如果您是Hello World php脚本,它应该出现在浏览器窗口中

<?php echo "Hello World!"; ?>

then it will spit that out in the browser. So your second code would look like

然后它会在浏览器中吐出来。所以你的第二个代码看起来像

<?php include("helloWorld.php");  echo " PHP ROCKS";?>

resulting in a page that would look like,

导致页面看起来像,

Hello world! PHP ROCKS

你好,世界! PHP ROCKS

#5


0  

To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.

为了避免在这个领域中陈述PHP的问题,为什么不将它放在shell脚本中呢?然后,PHP可以执行shell脚本,该脚本具有内部处理的所有重定向。

If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?

如果您需要动态更改内容,那么为什么不编写shell脚本然后执行它(当然,事后清理)?

#6


0  

This runs as if you run the script from browser.

这就像从浏览器运行脚本一样。

This came across while working on a project on linux platform.

这是在linux平台上开展项目时遇到的。

exec('wget http://<url to the php script>)

Hope this helps!!

希望这可以帮助!!

#1


28  

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:

我也有这个问题,事实证明这是php中的一个错误(#11430)。修复是在php脚本中调用另一个php脚本时使用php-cli。所以你仍然可以使用exec,而不是在浏览器中调用它时使用php使用php-cli:

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

This worked for me.

这对我有用。

#2


16  

What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.

exec正在做的是采取最右边的命令并将其附加到目的地。如果你的PHP脚本中有shebang行,你不需要包含php解释器的二进制指令。

if you just want the script's output, try:

如果您只想要脚本的输出,请尝试:

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

however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:

但是,如果您不希望错误出现在文件中,则应在输出到文件之前重定向STDERR。像这样:

exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')

the above should only output the "Hello World" to the execoutput.

以上应该只向execoutput输出“Hello World”。

Edit:

Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:

有趣的是你得到这种行为。你声明命令“ls”有效。尝试为此创建别名并将其转发到如下文件:

alias pexec='php /home/quote2bi/tmp/helloworld.php'

别名pexec ='php /home/quote2bi/tmp/helloworld.php'

then

exec('pexec > /tmp/execoutput.txt 2>&1 &')

it seems to be a problem with the way exec handles input as opposed to the shell itself.

它似乎是exec处理输入的方式的问题,而不是shell本身。

-John

#3


8  

The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.

问题出在PHP本身,它将所有内容视为脚本中的$ argv。它不会将输出重定向到文件ou / dev / null。

I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:

前段时间我遇到了同样的问题。我所做的是在/ opt / php-bin中创建一个runscript.php,然后在这个脚本中运行它应该运行的内容。像这样的东西:

$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd    = "{$script} {$params} > /dev/null &";

$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);

exit((int)$return);

And then you call it using:

然后你用它来调用它:

exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')

It´s the only way I managed to get this working.

这是我设法让这个工作的唯一方法。

#4


1  

if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like

如果你只是简单地运行一个php脚本,一种可能的方法来执行整个代码就是使用include()来运行php文件并输出任何结果。您不能将输出定向到文本文件,但如果您是Hello World php脚本,它应该出现在浏览器窗口中

<?php echo "Hello World!"; ?>

then it will spit that out in the browser. So your second code would look like

然后它会在浏览器中吐出来。所以你的第二个代码看起来像

<?php include("helloWorld.php");  echo " PHP ROCKS";?>

resulting in a page that would look like,

导致页面看起来像,

Hello world! PHP ROCKS

你好,世界! PHP ROCKS

#5


0  

To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.

为了避免在这个领域中陈述PHP的问题,为什么不将它放在shell脚本中呢?然后,PHP可以执行shell脚本,该脚本具有内部处理的所有重定向。

If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?

如果您需要动态更改内容,那么为什么不编写shell脚本然后执行它(当然,事后清理)?

#6


0  

This runs as if you run the script from browser.

这就像从浏览器运行脚本一样。

This came across while working on a project on linux platform.

这是在linux平台上开展项目时遇到的。

exec('wget http://<url to the php script>)

Hope this helps!!

希望这可以帮助!!