是否有可能运行两个命令并在同一页面上处理两个输出?


Is it possible to run two commands and deal with both outputs on same page

我在php中使用popen()运行两个linux命令。如果我运行1,我可以收集输出并很好地处理它。如果我同时运行两个,同时输出到2>&1的页面,输出就会变得混乱。是否有可能运行两个命令并在同一页面上处理两个输出?

我基本上复制了每个命令的底部代码

$handle = popen ("-some long command 2>&1");
while (false !== ($char = fgetc($handle)))
{
  if ($char == "'r")
  {
    // You could now parse the $line for status information.
$line= "$line'n";
if (preg_match("/Duration: (.*?),/", $line, $matches)){
//do stuff
}
$line = "";
} else {
$line .= $char;
}
ob_flush();
flush();
}
pclose ($handle);
}

proc_open允许多个异步命令执行;您将通过stdout管道获得输出。

PS:永远不要重复代码;用函数代替!