php exec/shell_exec/system/popen/proc_open 在 linux 上无限次运行调用脚本本身

标签 php linux shell exec

我有一个脚本,它使用 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():无法在/home/user/public_html/exec.php 中 fork [php -l/tmp/PHPFileI4T43l] 5"。 我认为这是递归的 exec 命令本身在每次递归时创建一个新进程,但无法找到它的原因。

最佳答案

经过 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 中找到相同问题的解决方案。

关于php exec/shell_exec/system/popen/proc_open 在 linux 上无限次运行调用脚本本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22225522/

相关文章:

linux - 在 linux 脚本中使用变量 Ping

Bash 语法错误,如 `syntax error: operand expected (error token is "{1 % 2 ")`

php - 统计: Compare previous value with actual value,数据库结构

php - MySql 中的规范化表结构...有点像?

php - 在 POST 请求中使用 multipart 时,Guzzlehttp 提示 header

linux - 修复 pacman 中的 "Target not found"(可能是 pacman.conf 的镜像列表

linux - 为什么 if 语句总是为真?

bash - 在 shell 脚本中对多个非标准列进行排序

php - 使用 DBH 在 PHP 中循环 MySQL 查询

c - x86 程序集中出现奇怪的段错误