简体   繁体   English

PHP 显示实时 Shell Output Z4C4AD5FCA2E7A3AAF74DBB1CED00381 内

[英]PHP Show Real Time Shell Output within HTML

I have a shell file shell.sh that echo hi every 1 second.我有一个 shell 文件 shell.sh 每 1 秒回显一次。 I would like to display the real time output of my shell script on my website in a box.我想在我的网站上的框中显示我的 shell 脚本的实时 output。

However, the problem is that, my code doesn't show the output in a box.但是,问题是,我的代码没有在框中显示 output。 And when I click on it, everything disappears and I just see a white page.当我点击它时,一切都消失了,我只看到一个白页。

The following is my php file.以下是我的 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>

and below is my shell script以下是我的 shell 脚本

while true
do 
    echo "Hi"
    sleep 1
done

How do I make my code run in the box?如何让我的代码在盒子中运行? And make sure the outputs just stay in the box and doesn't overflow.并确保输出只是留在盒子里并且不会溢出。 Why doesn't my code show the output?为什么我的代码不显示 output?

I am monitoring the server address which my php file is running.我正在监视我的 php 文件正在运行的服务器地址。 And when I click submit it loads forever on a white screen and since I know it's not gonna end I stop within the browser.当我点击提交时,它会永远加载在白屏上,因为我知道它不会结束,所以我在浏览器中停下来。 and I get the following error.我收到以下错误。

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

How can I show my real time shell output in a box?如何在盒子中显示我的实时 shell output? and it doesn't outflow the box border?并且它不会流出框边框?

If you are ok with using a node tool, and not pure php, checkout websocketd and this question .如果您可以使用节点工具,而不是纯粹的 php,请查看websocketd这个问题

PHP is not the best choice for a realtime app, because generally php apps are short lived, but what you are asking for is along the lines of a WebSocket server. PHP 不是实时应用程序的最佳选择,因为通常 php 应用程序寿命很短,但您要求的是 WebSocket 服务器。 Alternatively, you could use polling to call a php endpoint that reads from a log file, if you really want to use php.或者,如果您真的想使用 php,您可以使用轮询来调用从日志文件读取的 php 端点。

However, it is possible , just not as easy as using node (or elixir, or go, or ruby, etc.), when so many more examples for this use case exist for node.但是,当此用例存在更多示例时, 它可能不像使用节点(或 elixir、或 go、或 ruby 等)那么容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM