I have a terminal , and i run some shell script there, the shell script is execute some text every 5 second to terminal, then, i want php get text from text that are executed by shell script to terminal,then execute it to website, how to doit?
1 Answers
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>

- 1
- 1

- 997
- 9
- 22