dtvx3420 2017-02-28 21:28
浏览 307
已采纳

从php运行shell脚本并查看完整进度?

Is there a way to run a shell script from PHP and echo the results after progress is completed?

Here is my shell script:

(Its multilines - a few commands that have to be ran one after the other. )

cd
cd /var/www/html/
npm uninstall gulp --save
npm install gulp --save
npm start

here's my currently functioning PHP script. It only outputs some of the progress and only outputs it when complete. I need a live preview of the progress.

    <?php
echo "loaded<br><br>";
 // echo shell_exec("cd /var/www/html/..");
// rin the shell scrip from header_register_callback
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('cd /var/www/html/; npm start', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
  • 写回答

2条回答 默认 最新

  • duanqiao1947 2017-03-01 22:54
    关注

    Here is another approach. It uses redirection to output the results and then file_get_contents() to read the output.

    <?php
    
    echo "loaded<br><br>";
    
    $cmd = 'npm start';
    $run = $cmd . ' > result.txt 2> errors.txt';
    $output = shell_exec($run);
    
    $results = file_get_contents('result.txt');
    if (strlen(file_get_contents('errors.txt')) != 0) {
      $results .= file_get_contents('errors.txt');
    }
    
    echo "<pre>$results</pre>";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播