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条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据