agaggi agaggi - 6 days ago 6
Bash Question

php exec or shell_exec not terminating its process after script execution ends in linux

I am trying to check for syntax error of a php file using shell commands, its working fine in Windows (WAMP) but on linux the process created by shell commands exec/shell_exec/popen etc never terminates thus causing apache to hang while this process gets forcefully killed by me. also no output is generated after killing the process.

my test script is

file test.php is sample one line php file for testing only which contains

<?php
$test['arr'] = 'bla';
?>


and the code by which i try to check syntax error is:

$slash = file_get_contents('test.php');
$tmpfname = tempnam("tmp", "PHPFile");
file_put_contents($tmpfname, $slash);
exec("php -l ".$tmpfname." 2>&1",$error); //also tried shell_exec but same behaviour
$errtext = '';
foreach($error as $errline) $errtext.='<br>'.$errline;
unlink($tmpfname);
echo $errtext;


also tried using function popen

$slash = file_get_contents('test.php');
$tmpfname = tempnam("tmp", "PHPFile");
file_put_contents($tmpfname, $slash);
$handle = popen("php -l ".$tmpfname." 2>&1", 'r');
$errtext = fread($handle, 2096);
pclose($handle);
unlink($tmpfname);
echo $errtext;


please someone point me where i am doing wrong and why process created by shell commands never ends itself in linux, i tried searching lot about this problem but i get no result.

Answer

I got the root cause of the blocking issue, It was php session which was blocking all other requests in linux server from the same user (surprisingly on windows it works well).

I used session_write_close(); before running exec and problem resolved, but now i have one another problem with this script in linux, it is posted in another question php exec/shell_exec/system/popen/proc_open runs calling script itself infinite number of times on linux

Comments