首页 > 解决方案 > Laravel 命令执行输出

问题描述

我正在尝试制作将执行非结束脚本的工匠命令(更准确地说,它是一个 gulp watch 命令)。是否可以实时向控制台输出内容而不等待脚本结束?

exec($basedir . "test.sh", $output);

foreach ($output as $item) {
    $this->info($item);
}

test.sh看起来像这样(为简洁起见缩短):

echo "something"
echo "something else"

gulp watch

由于命令实际上并未结束,因此我无法检索在其中运行的两个回声。有没有办法做到这一点?

标签: phplaravelbashshelllaravel-artisan

解决方案


我通过执行以下操作解决了它:

while (@ ob_end_flush()) ; // end all output buffers if any

// this section streams output to terminal 
$proc = popen($basedir . "test.sh", 'r');
while (!feof($proc)) {
    $this->info(fread($proc, 4096));
    @ flush();
}

推荐阅读