如何使用PHP的实时反馈运行shell脚本?
我如何在向浏览器提供持续/实时反馈的同时从PHP执行Shell脚本? 我从系统函数文档中了解到:
How would I execute a shell script from PHP while giving constant/live feedback to the browser? I understand from the system function documentation:
system()调用还尝试自动刷新Web服务器的 如果PHP作为服务器运行,则在每行输出之后的输出缓冲区 模块.
The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.
我不清楚将其作为服务器模块"运行的含义.
I'm not clear on what they mean by running it as a 'server module'.
示例PHP代码:
<?php
system('/var/lib/script_test.sh');
示例shell代码:
#!/bin/bash
echo "Start..."
for i in {1..10}
do
echo "$i..."
sleep 1
done
echo "Done."
这是做什么的:它将等待大约10秒钟,然后刷新到输出缓冲区.
What this does: It will wait about 10 seconds and then flush to the output buffer.
我要这样做:每行输出后刷新到输出缓冲区.
What I want this to do: Flush to the output buffer after each line of output.
可以使用 popen()可为您打开的任何进程的标准输出提供句柄.可以使用 ob_flush()将数据块发送到客户端数据可以使用XHR显示.
This can be done using popen() which gives you a handle to the stdout of whatever process you open. Chunks of data can be sent to the client using ob_flush(), the data can be displayed using an XHR.