简体   繁体   中英

PHP Show Real Time Shell Output within HTML

I have a shell file shell.sh that echo hi every 1 second. I would like to display the real time output of my shell script on my website in a box.

However, the problem is that, my code doesn't show the output in a box. And when I click on it, everything disappears and I just see a white page.

The following is my php file.

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

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?

I am monitoring the server address which my php file is running. 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? 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 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. Alternatively, you could use polling to call a php endpoint that reads from a log file, if you really want to use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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