PHP文件执行带有连续输出的shell命令
问题描述:
Running shell commands from PHP is done like this:
<?php
$cmd = 'du /';
print shell_exec ( $cmd );
?>
Or
<?php
$cmd = 'du /';
passthru ( $cmd );
?>
However you only see the output when the job is done, how do you get continuous output? The du command above will take a period of time to complete, I'd like to see the output live.
My use case is running the CD ripper and tagger ABCDE, I'd like less technical people to be able to go to a web page to start a rip job but be able to see its progress.
(Lastly, is there a nice way to wrap this with screen or tmux so if the page load terminates the job won't)
答
Use proc_open for this. You can then read either stdout or stderr separately as data is being written to the streams.