如何使用PHP的实时反馈运行shell脚本? [英] How to run shell script with live feedback from PHP?

查看:110
本文介绍了如何使用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.

这篇关于如何使用PHP的实时反馈运行shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆