2016-12-09 19 views
0

laravelコントローラの関数で実行されるbashスクリプトがあり、スクリプトの実行中にその中からエコーを出力したい。私はこの方法で試してみましたが、うまくいきませんでした。bashスクリプトを実行中にLaravel 5.3でリアルタイムログを表示する

これは私がアヤックス

$.ajax({ 
     data: parameters, 
     url: '/executeBash', 
     type: 'get', 
     beforeSend: function(){ 
      console.log("executing..."); 
     }, 
     success: function(response){ 
      console.log("success: %O", response); 
     }, 
     error: function(response){ 
      console.log("error: %O", response); 
     } 
    }); 

でこれはAjaxの機能

Route::get('/executeBash', '[email protected]_bash'); 
によって呼び出さweb.php Laravelルートファイルで私のルート文であることを行うコントローラ

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use File; 
use Process; 
use ProcessFailedException; 

class DeployController extends Controller 
{ 
    // 
    public function execute_bash(command) 
    { 
     flush(); 
     $fp = popen(command, "r"); 
     while(!feof($fp)) 
     { 
      // send the current file part to the browser 
      print fread($fp, 1024); 
      // flush the content to the browser 
      flush(); 
     } 
     fclose($fp); 

    } 

です

execute_bash関数のコマンドパラメータは、内部でいくつかのものを実行する.shスクリプトへのパスです。それは、エコーで出力を印刷します。それは

どのように私は私が欲しいWathのを達成することができますを終了した後

さて、.SHスクリプトは、右実行されているが、すべての出力(エコー)を示していますか?ありがとう!

+0

もう少し詳しく説明できますか?たとえば、コマンドサンプルと成功コールバックの出力を追加できますか? – Hackerman

+0

@Hackerman私は投稿を編集しました! – Quetool

+0

私はそれを手に入れました。あなたは、リアルタイムのシナリオのようにあなたのスクリプトの出力を表示したい...今すぐ出力データは、スクリプトの実行が終了したら表示されますか? – Hackerman

答えて

1

[OK]を、私は次の操作を行って、XHRのmethosでこれを達成:

を、これは私の新しいAjaxの機能(XHR法)

function executeBash(){ 

    if (!window.XMLHttpRequest){ 
     alert("Your browser does not support the native XMLHttpRequest object."); 
     return; 
    } 
    try{ 
     var xhr = new XMLHttpRequest(); 
     xhr.previous_text = ''; 

     xhr.onerror = function() { 
      alert("[XHR] Fatal Error."); 
     }; 
     xhr.onreadystatechange = function() { 
      try{ 
       if (xhr.readyState == 4){ 
        //alert('[XHR] Done') 
        document.getElementById("timeline").innerHTML += 'Done' + ''; 
       } 
       else if (xhr.readyState > 2){ 
        console.warn(xhr.responseText); 
        var new_response = xhr.responseText.substring(xhr.previous_text.length); 
        document.getElementById("timeline").innerHTML += new_response + ''; 
        xhr.previous_text = xhr.responseText; 
       } 
      } 
      catch (e){ 
       alert("[XHR STATECHANGE] Exception: " + e); 
      } 
     }; 
     xhr.open("GET", "/executeBash", true); 
     xhr.send(); 
    } 
    catch (e){ 
     alert("[XHR REQUEST] Exception: " + e); 
    } 

} 

であり、これは私のLaravelコントローラ

の新機能であります
public function execute_bash(command) 
    { 
     header("Connection: Keep-alive"); 

     $fp = popen(command, "r"); 
     while($b = fgets($fp, 2048)) { 
      echo $b."<br>"; 
      flush(); 
     } 

     pclose($fp); 
    } 

これはうまく動作します。

は、私がこの記事

http://stratosprovatopoulos.com/web-development/php/ajax-progress-php-script-without-polling/

そしてこの答え

http://www.webhostingtalk.com/showthread.php?t=579738&p=4333537#post4333537

私はそれが役に立てば幸いを読むことによって液に到着しました!

+0

ニースハック@Quetool :) – Hackerman

関連する問題