【问题标题】:php exec or shell_exec not terminating its process after script execution ends in linuxphp exec或shell_exec在linux中脚本执行结束后没有终止其进程
【发布时间】:2014-03-05 18:15:56
【问题描述】:

我正在尝试使用 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 中永远不会结束,我尝试了很多关于这个问题的搜索,但我没有得到任何结果。

【问题讨论】:

    标签: php linux shell


    【解决方案1】:

    我得到了阻塞问题的根本原因,它是 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

    【讨论】:

      猜你喜欢
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 2013-01-13
      • 2021-09-16
      • 1970-01-01
      • 2013-11-22
      相关资源
      最近更新 更多