php exec/shell_exec/system/popen/proc_open在Linux上无限次数地运行调用脚本本身 [英] php exec/shell_exec/system/popen/proc_open runs calling script itself infinite number of times on linux

查看:125
本文介绍了php exec/shell_exec/system/popen/proc_open在Linux上无限次数地运行调用脚本本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本使用php -l检查php文件中的语法错误.它在Windows中工作正常,但在Linux中给出错误的输出:

I've a script which check for syntax error in php file using php -l . It works fine in windows but gives incorrect output in Linux:

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

content of file exec_ip.php which is being checked for syntax error is (it has syntax error which is to be checked):

<?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)中的结果{正确}:

RESULT IN WINDOWS (WAMP) {CORRRECT}:

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){未知的输出}:

RESULT IN LINUX (Centos/cPanel) {UNKNOWN OUTPUT}:

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,但是所有行为都相同.我正在尝试追踪过去两天的根本原因...请帮助

Please someone help me and point me why it is giving incorrect output in linux production server. I've also tried using shell_exec, popen, proc_open, system in place of exec but all has same behavior. I am trying to trace the root cause from past 2 days... please help

有时我会看到以下错误日志"PHP警告:exec():无法在/home/user/public_html/exect.php中的[php -l/tmp/PHPFileI4T43l]分叉5. 我认为这是递归的exec命令本身,每次递归都会创建一个新进程,但无法找到原因.

Sometimes i see following errorlog "PHP Warning: exec(): Unable to fork [php -l /tmp/PHPFileI4T43l] in /home/user/public_html/exect.php on line 5". I think it is recursing exec commands itself creating a new process on each recursion, but couldn't get the cause of it.

推荐答案

经过2天的头痛和大量的谷歌搜索……我在链接

After 2 days of headache and lots of googling... i found the solution in the link 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版本,它从环境中读取脚本名称,从而导致我的调用脚本运行无限次,或者直到允许的最大进程数或直到整个内存被消耗为止.

It was PHP CGI version which reads script name from the environment thus causing my calling script to run infinite number of times or untill the maximum number of allowed process or till whole memory was consumed.

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

The solution as simply using command php-cli instead of command php.

我替换了我代码中的以下行

i replaced below line in my code

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

使用

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

现在一切都很好.

我希望它能对某人有所帮助. 我还更改了此问题的标题,以便其他人可以在google中轻松找到相同问题的解决方案.

I hope it'll help someone. I've also changed the title of this question so that others people can easily find the solution of same problem in google.

这篇关于php exec/shell_exec/system/popen/proc_open在Linux上无限次数地运行调用脚本本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆