简体   繁体   English

PHP从Linux终端获取文本并回显它

[英]php Get text from linux terminal and echo it

我有一个终端,然后在其中运行一些Shell脚本,该Shell脚本每5秒执行一次到终端的文本,然后,我要php从Shell脚本执行的文本中获取文本到终端,然后将其执行到网站,怎么做?

One way is to use a file as a buffer. 一种方法是将文件用作缓冲区。 You would need to either edit your shell script, or call your script with an output redirection command. 您将需要编辑您的Shell脚本,或使用输出重定向命令调用您的脚本。 You could then use JavaScript (or AJAX) to dynamically load it into the page, without requiring a refresh. 然后,您可以使用JavaScript(或AJAX)将其动态加载到页面中,而无需刷新。

Shell output redirection Shell输出重定向

./my_shell_script.sh > /my/file/location 2>&1

The > is a redirection operator in Linux, you can read more on it here . >是Linux中的重定向运算符,您可以在此处阅读更多内容。 If you are following some particular type of formatting, you may need to use a different file for error outputting. 如果您遵循某种特定类型的格式设置,则可能需要使用其他文件来输出错误。 Change the 2>&1 to 2> /my/new/file/location , otherwise if an error occurs, it will be output into that file as well. 2>&1更改为2> /my/new/file/location ,否则如果发生错误,该错误也会输出到该文件中。

PHP load updated file information PHP加载更新的文件信息

This just reloads the log file (or buffer, in this case) and prints it. 这只是重新加载日志文件(在这种情况下为缓冲区)并打印出来。 The AJAX handles updating your page with the new information. AJAX会使用新信息来处理您的页面更新。

<?php

// check for call to new data
if(isset($_GET["updateResults"])){

    print file_get_contents("/myfile/location");

}

?>

AJAX using jQuery 使用jQuery的AJAX

Sends a call to your PHP script, which returns your updated data. 向您的PHP脚本发送调用,该脚本返回您的更新数据。 You then replace everything in your div with the updated data. 然后,用更新的数据替换div中的所有内容。 You will need the jQuery library, which you can get from here . 您将需要jQuery库,您可以从此处获得。

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

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

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