简体   繁体   English

无法使用PHP exec执行PHP脚本

[英]Can't execute PHP script using PHP exec

I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. 我试图使用PHP exec调用调用一个需要几秒钟(与第三方的Web服务)的脚本。 After much struggling, I reduced this to the classic hello world example. 经过多次努力,我把它减少到了经典的hello world例子。 The calling script looks like: 调用脚本如下所示:

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? 为什么我不能使用exec执行此PHP脚本? Note that when I change the command to something like ls -l , the output is a directory listing as expected. 请注意,当我将命令更改为ls -l类的命令时,输出是按预期方式列出的目录。 btw, in case it matters, I did chmod the called script to 755... 顺便说一下,如果重要的话,我把调用的脚本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. 更新 - 我将exec调用移动到调用脚本的末尾,至少现在我没有看到在输出中执行调用脚本。 Thx to posters and I will try some of these ideas. 感谢海报,我会尝试其中的一些想法。

Help! 救命!

Thanks Steve 谢谢Steve

I had this issue also and it turns out this is a bug in php (#11430). 我也遇到了这个问题,事实证明这是php中的一个错误(#11430)。 The fix is to use php-cli when calling another php script within a php script. 修复是在php脚本中调用另一个php脚本时使用php-cli。 So you can still use exec but rather than use php use php-cli when calling it in the browser: 所以你仍然可以使用exec而不是在浏览器中调用它时使用php使用php-cli:

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

This worked for me. 这对我有用。

What exec is doing is taking the rightmost command and appending it to your destination. exec正在做的是采取最右边的命令并将其附加到目的地。 If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter. 如果你的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. 但是,如果您不希望错误出现在文件中,则应在输出到文件之前重定向STDERR。 Like so: 像这样:

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. 你声明命令“ls”有效。 Try making an alias for this and forward it to a file like so: 尝试为此创建别名并将其转发到如下文件:

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 -约翰

The problem is with PHP itself, it treats everything as $argv in the script. 问题出在PHP本身,它将所有内容视为脚本中的$ argv。 It doesn´t redirect the output to a file ou to /dev/null. 它不会将输出重定向到文件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. 我所做的是在/ opt / php-bin中创建一个runscript.php ,然后在这个脚本里运行它应该运行的内容。 Something like this: 像这样的东西:

$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. 这是我设法让这个工作的唯一方法。

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. 如果你只是简单地运行一个php脚本,一种可能的方法来执行整个代码就是使用include()来运行php文件并输出任何结果。 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 您不能将输出定向到文本文件,但如果您是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

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!! 希望这可以帮助!!

To avoid the stated problems of PHP in this area, why not put this in inside a shell script? 为了避免在这个领域中陈述PHP的问题,为什么不将它放在shell脚本中呢? PHP can then execute the shell script which has all the redirections handled internally. 然后,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脚本然后执行它(当然,之后要清理)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM