从php运行的bash脚本的进度条


Progress bar for a bash script running from php

在StackOverflow上的人的帮助下,我成功地从php运行了一个bash脚本。现在一切都很顺利,但由于脚本需要几分钟才能结束,我希望看到进展。在bash脚本中,有一些printf命令输出错误/成功消息。

作为php的新手,我认为这些消息在bash脚本中执行时会出现。但是,尽管脚本已经完成,但我没有得到任何输出。我可以看到创建的文件,当然脚本也完成了,但在浏览器中看不到php的任何输出。

当脚本仍在运行时,我如何检查进度?为什么php似乎没有结束?

以下是php脚本。

<?php
echo "<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
      <html xmlns=http://www.w3.org/1999/xhtml>
      <head>
      <meta http-equiv=Content-Type content=text/html; charset=UTF-8 />
      <title>Title</title>
      <link href=../style.css rel=stylesheet type=text/css>
      </head>
      <body topmargin=0 leftmargin=0>";
echo '<font color=blue>';
echo '------------------------------------------------------------- <br>';
echo 'Inicio de la generación manual de mapas de olas de calor<br>';
echo 'Este proceso tarda varios minutos<br>';
echo '-------------------------------------------------------------<br>';
echo date('h:i:s') . "'n";
$output = shell_exec('/home/meteo/www/RAMS/olas/generar-mapas.bash');
echo "<pre>".$output."</pre>";
echo '</font>';
echo '</body>';
echo '</html>';
?>

我在论坛上读过一些关于php、Ajax和jQuery的帖子,但我只知道一些非常基本的php。有没有一个简单的php新手解决方案?感谢您的帮助

shell_exec()阻塞直到子进程完成,请使用popen()

$p = popen('/home/meteo/www/RAMS/olas/generar-mapas.bash', 'r');
while(!feof($p)) {
    echo fgets($p);
    ob_flush();
    flush();
}
pclose($p);