How does php display shell script output in real time?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.