Hello, how can php display the output of shell scripts in real time? I now have a shell script to implement the one-click startup function. To make it a web version
However, the script is executed for a long time, which may take about 2-3 minutes. during this period, some successful prompts will be returned to the screen.
But now I use php to call this script, using exec and system. I found that the statement will be printed on the webpage until all the scripts are executed.
Now, my problem is: can I print a statement by using shell? a statement is displayed on the webpage, instead of displaying it together after all the shell statements are executed?
Thank you!
Reply to discussion (solution)
Your echo statement is in shell.
You can add an output redirection when calling shell, and append a file. to call this shell, it must be executed in the background.
Then, use php to call the tail command to read the output file every second. here, you need to set the buffer to 0. Otherwise, the output file will be displayed only after 4096.
For example:
Exec ("/home/web/a. sh>/home/web/a. log 2> & 1 &");
$ Log = file_get_contents ('/home/web/a. log ');
Here, you can store each row into an array and then only echo the new row at a time.
Both exec and system are functions.
Since it is a function, it will definitely wait for the response
Unless the called function has a callback function
Since exec and system do not provide callback function interfaces, it is only waiting for the end of the interface.
Your echo statement is in shell.
You can add an output redirection when calling shell, and append a file. to call this shell, it must be executed in the background.
Then, use php to call the tail command to read the output file every second. here, you need to set the buffer to 0. Otherwise, the output file will be displayed only after 4096.
For example:
Exec ("/home/web/a. sh>/home/web/a. log 2> & 1 &");
$ Log = file_get_contents ('/home/web/a. log ');
Here, you can store each row into an array and then only echo the new row at a time.
Thank you for your reply. I will close the post after completing this step. If you have any questions, please kindly advise
Both exec and system are functions.
Since it is a function, it will definitely wait for the response
Unless the called function has a callback function
Since exec and system do not provide callback function interfaces, it is only waiting for the end of the interface.
Thank you for your reply. I will close the post after completing this step. If you have any questions, please kindly advise
Thanks to the two upstairs. The Post is late and the problem has been solved.
Similar to the method on the first floor
The script needs to be executed in the background, and then the output is redirected to the file.
Php regularly reads the newly added content in the file every 1 s while. here I recorded the file offset and then fseek to locate it.
Finally, when an output indicating successful script execution occurs, it jumps out of the while and ends.