我有一个终端,我在那里运行一些shell脚本,shell脚本每隔5秒执行一些文本到终端,然后,我希望php从shell脚本执行的文本中获取文本到终端,然后执行它到网站,怎么办?
答案 0 :(得分:2)
一种方法是使用文件作为缓冲区。您需要编辑shell脚本,或使用输出重定向命令调用脚本。然后,您可以使用JavaScript(或AJAX)将其动态加载到页面中,而无需刷新。
./my_shell_script.sh > /my/file/location 2>&1
>
是Linux中的重定向运算符,您可以在其上阅读更多here。如果您正在使用某种特定类型的格式,则可能需要使用其他文件进行错误输出。将2>&1
更改为2> /my/new/file/location
,否则如果发生错误,它也会输出到该文件中。
这只是重新加载日志文件(或缓冲区,在本例中)并打印它。 AJAX使用新信息处理页面更新。
<?php
// check for call to new data
if(isset($_GET["updateResults"])){
print file_get_contents("/myfile/location");
}
?>
发送对PHP脚本的调用,该脚本返回更新的数据。然后使用更新的数据替换div中的所有内容。您将需要jQuery库,您可以从here获得。
<div id="myDivToChange">No data yet!</div>
<script type="text/javascript">
$(document).ready(function(){
// refresh every minute (60 seconds * 1000 milliseconds)
setInterval(myFunction, 60000);
});
function myFunction(){
$.ajax({
type: "POST",
url: "./linkToMyPHP.php?updateResults=1",
success: function(data){
$("#myDivToChange").html(data);
}
});
}
</script>