【发布时间】:2020-05-07 20:58:10
【问题描述】:
我有一个 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 输出?并且不会溢出框边框?
【问题讨论】:
-
“实时”是指“实时更新”还是“页面加载时的当前状态”?
-
@GarrettMotzner 实时更新!输出实时实时更新。
-
好吧,好吧。那么,PHP 可能是一个糟糕的选择,因为你想要 websockets,而 PHP can 做 websockets,它不是一个自然的选择。 Node 可能是最简单的选择。
-
另外,您可以进行轮询,基本上每隔几秒读取一个日志文件并将该内容显示到页面,但 websockets 更适合您的用例。
-
满足您需求的节点示例:github.com/joewalnes/websocketd
标签: javascript php html bash shell