简体   繁体   中英

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. You could then use JavaScript (or AJAX) to dynamically load it into the page, without requiring a refresh.

Shell output redirection

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

The > is a redirection operator in Linux, you can read more on it here . 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.

PHP load updated file information

This just reloads the log file (or buffer, in this case) and prints it. The AJAX handles updating your page with the new information.

<?php

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

    print file_get_contents("/myfile/location");

}

?>

AJAX using jQuery

Sends a call to your PHP script, which returns your updated data. You then replace everything in your div with the updated data. You will need the jQuery library, which you can get from 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>

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