我有一个shell文件shell.sh,它每1秒回声一次。我想在一个框内的网站上显示我的shell脚本的实时输出。

但是,问题是,我的代码没有在框中显示输出。当我单击它时,所有内容都消失了,我只看到一个白页。

以下是我的php文件。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <form method="GET" action="splitter.php">
        <label for="folderPath">Folder Path:</label>
        <input type="text" id="folderPath" name="folderPath">
    <br></br>
        <fieldset id="khz">
            <p>Please Choose Khz: </p>
          <input type="radio" value="8khz" name="khz"> 8khz <br>
          <input type="radio" value="16khz" name="khz"> 16khz <br>
        </fieldset>
    <br></br>
        <fieldset id="audio-type">
            <p>Please Choose Type: </p>
          <input type="radio" value="C12" name="group2"> Channel 1 & 2 <br>
          <input type="radio" value="LR" name="group2"> Left to Right <br>
        </fieldset>
        <div class="spaceh10"></div>
        <input class = "submitButton" type="submit" value="Submit">

    <div class="spaceh10"></div>
    <div style="width:500px;height:100px;border:1px solid #000;">
    <?php
    if (isset($_GET['folderPath']) && !empty($_GET['folderPath']) &&
    isset($_GET['khz']) && isset($_GET['group2'])) {

    $folderPath = $_GET['folderPath'];
    $khz = $_GET['khz'];
    $group2 = $_GET['group2'];
    #$folderPath $khz $group2
    $command = './shell.sh';
    while (@ ob_end_flush()); // end all output buffers if any

    $proc = popen($command, 'r');
    echo '<pre>';
    while (!feof($proc)){
        echo fread($proc, 4096);
        @ flush();
    }
    echo '</pre>';
    }
    ?>
    </div>
</body>
</html>


下面是我的shell脚本

while true
do
    echo "Hi"
    sleep 1
done


如何使我的代码在框中运行?并确保输出仅停留在框内且不会溢出。为什么我的代码没有显示输出?

我正在监视运行我的php文件的服务器地址。当我单击提交时,它将永久加载在白屏上,并且由于我知道它不会结束,所以我将在浏览器中停止。
我得到以下错误。

./shell.sh: line 6: echo: write error: Broken pipe


如何在框中显示我的实时Shell输出?它不会流出框边框吗?

最佳答案

如果可以使用节点工具而不是纯PHP,请签出websocketdthis question

PHP并不是实时应用程序的最佳选择,因为通常php应用程序寿命短,但是您所要求的却是WebSocket服务器。另外,如果您确实想使用php,则可以使用轮询来调用从日志文件读取的php端点。

但是,it is possible并不像使用节点(或长生不老药,去吧,去做或红宝石等)那样容易,这是因为节点中存在许多此用例的示例。

09-20 14:09