在命令仍在运行时从php exec()获取结果? [英] Grab results from a php exec() while the command is still running?
问题描述
当我像这样从PHP运行exec
时:
When I run an exec
from PHP like so:
$result = exec('command');
由此产生的结果将存储在$result
中.但是在我目前的情况下,我的命令可能需要花费几分钟并在运行时输出结果.有没有办法在运行时获取输出?我知道passthru
方法会将结果输出为浏览器,但实际上我直接想要它.
The results from this will be stored in $result
. But in my current case, my command can take a few minutes and outputs results as it is running. Is there a way I can get output while it is running? I know that the passthru
method will output the results to be browser, but I actually want it directly.
推荐答案
您应该看看 proc_open
在使输出流成为非阻塞(使用stream_set_blocking
)之后,您可以随时读取它,而不会阻塞PHP代码.
After making the output stream non-blocking (with stream_set_blocking
), you can read from it whenever you want without having your PHP-code blocked.
-编辑- 如果您使用
$result = exec('command > /path/to/file &');
它将在后台运行,您可以在/path/to/file中读取输出
It will run in the background and you can read the output in /path/to/file
这篇关于在命令仍在运行时从php exec()获取结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!