【问题标题】:php-capture console output while processing a lengthy script处理冗长脚本时的 php-capture 控制台输出
【发布时间】:2011-02-08 11:34:26
【问题描述】:

我在将 ssh 脚本输出捕获到浏览器上时遇到了问题,因为它执行而不是最后。

脚本编写:

$descriptorspec = array(
        0 => array("pipe","r"),
        1 => array("pipe","w"),
        2 => array("file","./error.log","a")
) ;
$cwd = 'path/path1' ;
for($counter=1;$counter<= 10;$counter++)
{
        $cmd="sudo test.sh arg1 arg2 arg3";
        $process = proc_open('ssh  user@server', $descriptorspec, $pipes, $cwd) ;
        if (is_resource($process))
        {
          fwrite($pipes[0], $cmd) ;
         fclose($pipes[0]) ;
                echo stream_get_contents($pipes[1]) ;
                  fclose($pipes[1]) ;
                $return_value = proc_close($process);
            echo "$counter=command returned $return_value<br>";
        }
}

Shell 脚本需要 10 分钟才能执行。如果 $counter=10 则需要花费太多时间才能将实际输出显示在屏幕上。我要求它在执行时继续显示流输出,以便我们知道发生了什么。 有缓冲的情况吗?

【问题讨论】:

    标签: php streaming continuous proc-open


    【解决方案1】:

    试试flush() function

    【讨论】:

    • 谢谢。你的意思是说- echo stream_get_contents($pipes[1]) ;冲洗();
    • 谢谢。你的意思是说- echo stream_get_contents($pipes[1]) ;冲洗();我试过这个 - if (ob_get_level() == 0) ob_start(); echo stream_get_contents($pipes[1]) ; ob_flush();冲洗();睡眠(50000);如果 script1 花费的时间更短,它工作得很好,但是对于需要时间执行的 script2,它会遇到同样耗时的问题。
    猜你喜欢
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多