dongyun65343 2017-04-07 08:45
浏览 62
已采纳

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

I have a PHP script which prints the output of a bash script (actually it is an expect script), which looks like this:

<?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>';
?>

So I wanted to get the real time output with automatic scroll at the end of the page with every new line appearance and I found this: printing process output in realtime

Then I added the proposed html code to my script as follows:

<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>

However, when script finishes the web browser won't let me browse to the top of the page because it is still scrolling to the bottom.

How shall avoid it so that I can browse once the script is finished?

  • 写回答

1条回答 默认 最新

  • dszsajhd237437 2017-04-07 09:00
    关注

    you are using setInterval to repeat some task and never ask it to stop that.

    you will need to stop setInterval from repeating itself , at the end of your php code add :

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

    also you will need to close your proc process :

    if (is_resource($process)) {
        while ($s = fgets($pipes[1])) {
            print $s;
    
        }
        proc_close($process);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd