脚本执行在linux结束后,php exec或shell_exec不会终止其进程

时间:2014-03-05 18:15:57

标签: php linux shell

我正在尝试使用shell命令检查php文件的语法错误,它在Windows(WAMP)中正常工作但在linux上由shell命令exec / shell_exec / popen等创建的进程永远不会终止因此导致apache挂起而这个过程被我强行杀死了。杀死进程后也没有生成输出。

我的测试脚本是

文件test.php是样本一行的php文件,仅用于测试包含

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

和我尝试检查语法错误的代码是:

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

也尝试使用函数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;

请有人指出我在哪里做错了,为什么shell命令创建的进程永远不会在linux中结束,我试着搜索很多关于这个问题,但我没有得到任何结果。

1 个答案:

答案 0 :(得分:2)

我找到了阻塞问题的根本原因,这是一个php会话阻止了同一个用户在Linux服务器上的所有其他请求(令人惊讶的是在Windows上运行良好)。

我使用了session_write_close();在运行exec和问题解决之前,但现在我在linux中有这个脚本的另一个问题,它发布在另一个问题php exec/shell_exec/system/popen/proc_open runs calling script itself infinite number of times on linux