【问题标题】:php exec/shell_exec/system/popen/proc_open runs calling script itself infinite number of times on linuxphp exec/shell_exec/system/popen/proc_open 在 linux 上无限次运行调用脚本本身
【发布时间】:2014-04-09 03:36:10
【问题描述】:

我有一个脚本,它使用 php -l 检查 php 文件中的语法错误。它在 Windows 中运行良好,但在 Linux 中输出不正确:

正在检查语法错误的文件exec_ip.php的内容是(它有要检查的语法错误):

<?php
$arr['12] = 'asd';
?>

脚本是:

$slash = file_get_contents('exec_ip.php');
//echo $slash;
$tmpfname = tempnam("tmp", "PHPFile");
file_put_contents($tmpfname, $slash);
exec("php -l ".$tmpfname,$error);

$errtext = '';      
foreach($error as $errline) $errtext.='<br>'.$errline;          
unlink($tmpfname);
echo 'ERR:'.$errtext;

结果在 WINDOWS (WAMP) {正确}:

ERR:

Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\WINDOWS\Temp\PHP1F1.tmp on line 2

Errors parsing C:\WINDOWS\Temp\PHP1F1.tmp

LINUX (Centos/cPanel) {未知输出}:

ERR:
Content-type: text/html

ERR:
Content-type: text/html

ERR:
Content-type: text/html

ERR:
Content-type: text/html

ERR:
Content-type: text/html

ERR:
Content-type: text/html

ERR:
Content-type: text/html

ERR:
Content-type: text/html

ERR:
... too many same above lines

请有人帮助我并指出为什么它在 linux 生产服务器中给出不正确的输出。我也尝试过使用 shell_exec、popen、proc_open、system 代替 exec,但它们都有相同的行为。我正在尝试追踪过去 2 天的根本原因...请帮助

编辑: 有时我看到以下错误日志“PHP 警告:exec(): Unable to fork [php -l /tmp/PHPFileI4T43l] in /home/user/public_html/exect.php 在线5"。 我认为它是递归 exec 命令本身在每次递归时创建一个新进程,但无法得到它的原因。

【问题讨论】:

    标签: php linux shell exec


    【解决方案1】:

    经过 2 天的头痛和大量谷歌搜索...我在链接 http://www.mombu.com/php/php-5-forum/t-24759-exec-or-system-et-all-cause-an-infinite-loop-of-starting-requested-program-8469354.html 中找到了解决方案

    它是 PHP CGI 版本,它从环境中读取脚本名称,从而导致我的调用脚本运行无限次或直到允许的最大进程数或直到耗尽整个内存。

    解决方案是简单地使用命令 php-cli 而不是命令 php。

    我在我的代码中替换了下面的行

    exec("php -l ".$tmpfname,$error);
    

    exec("php-cli -l ".$tmpfname,$error);
    

    现在一切都很好。

    我希望它会帮助别人。 我还更改了这个问题的标题,以便其他人可以在 google 中轻松找到相同问题的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 2013-01-13
      • 2015-11-20
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多