从PHP调用的bash脚本输出中自动滚动

时间:2017-04-07 08:46:00

标签: php html bash

我有一个PHP脚本打印bash脚本的输出(实际上它是一个期望脚本),如下所示:

<?php

ob_implicit_flush(true);
ob_end_flush();

$cmd = "./expect_script.sh";

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "w") 
);

$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());

echo '<pre>';
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;

    }
}
echo '</pre>';
?>

所以我希望通过页面末尾的自动滚动获得实时输出,每个新的线条外观我发现:printing process output in realtime

然后我将建议的html代码添加到我的脚本中,如下所示:

<html><head>
<script language="javascript">
var int = self.setInterval("window.scrollBy(0,1000);", 200);
</script>
</head>
<body>

<?php

ob_implicit_flush(true);
ob_end_flush();

$cmd = "./expect_script.sh";

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "w") 
);

$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());

echo '<pre>';
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;

    }
}
echo '</pre>';
?>

</body>
</html>

然而,当脚本完成时,Web浏览器不会让我浏览到页面顶部,因为它仍然滚动到底部。

如何避免它以便我可以在脚本完成后浏览?

1 个答案:

答案 0 :(得分:1)

您正在使用setInterval重复某项任务,并且从不要求它停止此操作。

你需要停止setInterval重复自己,在php代码的末尾添加:

echo '</pre>';
echo '<script language="javascript">self.clearInterval(int);</script>';

您还需要close your proc处理:

if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;

    }
    proc_close($process);
}