开发者

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

开发者 https://www.devze.com 2023-03-21 06:43 出处:网络
I am running two linux commands with popen() in php. If I run 1, I can gather the output and process it fine. If I run two at the same time while funneling to the page with 2>&1, output gets jumbl

I am running two linux commands with popen() in php. If I run 1, I can gather the output and process it fine. If I run two at the same time while funneling to the page with 2>&1, output gets jumbled. Is it possible to run two commands and deal with both outputs on same page?

I basically duplicated the bottom code for each command

$handle = popen ("-some lon开发者_Go百科g 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 allows multiple, asynchronous commands execution; you would get the output through the stdout pipe.

PS: never duplicate code; use functions instead!

0

精彩评论

暂无评论...
验证码 换一张
取 消