Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php exec/shell_exec/system/popen/proc_open runs calling script itself infinite number of times on 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:

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';
?>

and the script is:

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

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

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

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

EDIT: 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.

like image 760
Dr. DS Avatar asked Dec 25 '22 12:12

Dr. DS


1 Answers

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

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.

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

i replaced below line in my code

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

with

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

and now everything is fine.

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.

like image 82
Dr. DS Avatar answered Apr 27 '23 05:04

Dr. DS