无法使用 PHP exec 执行 PHP 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/566248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:06:52  来源:igfitidea点击:

Can't execute PHP script using PHP exec

phpexec

提问by John T

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 调用调用一个需要几秒钟的脚本(具有 3rd 方的 Web 服务)。经过一番挣扎,我将其简化为经典的 hello world 示例。调用脚本如下所示:

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

When I run this, the output execoutput.txtcontains a copy of the invoking script page, not hello worldas 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...

为什么我不能让这个 PHP 脚本使用 exec 来执行?请注意,当我将命令更改为类似 时ls -l,输出是预期的目录列表。顺便说一句,以防万一,我将被调用的脚本修改为 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

谢谢史蒂夫

回答by Keith

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.

这对我有用。

回答by John T

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.

上面应该只输出“Hello World”到execoutput。

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'

alias 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

-约翰

回答by Luiz Damim

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。它不会将输出重定向到 /dev/null 的文件。

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

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

$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.

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

回答by Nathan

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岩石

回答by Amar

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!!

希望这可以帮助!!

回答by Jeremy L

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 脚本然后执行它(当然,之后还要清理)?