2011-03-29 195 views
0

美好的一天。從php腳本執行linux命令

我需要發送一個請求到一個腳本,然後執行一些命令(基本上我需要點擊鏈接時啓動ffmpeg)。

我嘗試這樣做:

exec("ffserver &"); 
exec("ffmpeg -i pipe.avi output.avi"); 
exec("mplayer -dumpstream someinput -dumpfile pipe.avi"); 

shell_exec("ffserver &"); 
shell_exec("ffmpeg -i pipe.avi output.avi"); 
shell_exec("mplayer -dumpstream someinput -dumpfile pipe.avi"); 

在這兩種情況下,我得到了超時。任何1可以幫助我嗎?謝謝。

+0

注意:似乎在運行shell變體之後,無論我做什麼,我都會發生超時:|所以......任何一個也可以添加一個筆記我如何修復我的PHP解析器? (如果不是我只是重新安裝apache) – zozo 2011-03-29 08:58:10

回答

1

你可以試試這個,它已經發布的用戶巴赫裏在巴赫裏點信息exec

<?php 
    function PsExecute($command, $timeout = 60, $sleep = 2) { 
     // First, execute the process, get the process ID 

     $pid = PsExec($command); 

     if($pid === false) 
      return false; 

     $cur = 0; 
     // Second, loop for $timeout seconds checking if process is running 
     while($cur < $timeout) { 
      sleep($sleep); 
      $cur += $sleep; 
      // If process is no longer running, return true; 

      echo "\n ---- $cur ------ \n"; 

      if(!PsExists($pid)) 
       return true; // Process must have exited, success! 
     } 

     // If process is still running after timeout, kill the process and return false 
     PsKill($pid); 
     return false; 
    } 

    function PsExec($commandJob) { 

     $command = $commandJob.' > /dev/null 2>&1 & echo $!'; 
     exec($command ,$op); 
     $pid = (int)$op[0]; 

     if($pid!="") return $pid; 

     return false; 
    } 

    function PsExists($pid) { 

     exec("ps ax | grep $pid 2>&1", $output); 

     while(list(,$row) = each($output)) { 

       $row_array = explode(" ", $row); 
       $check_pid = $row_array[0]; 

       if($pid == $check_pid) { 
         return true; 
       } 

     } 

     return false; 
    } 

    function PsKill($pid) { 
     exec("kill -9 $pid", $output); 
    } 
?> 
+0

我會在一分鐘內嘗試它...剛修復apache後:)。 – zozo 2011-03-29 09:03:43

1

PHP手冊條目的註釋。如果這只是一個超時錯誤,請嘗試將參數或者set_time_limit (XX);在你的代碼之上。 xx對應於以秒爲單位等待的時間。

把0表示沒有時間限制,但如果你的腳本進入無限循環,如果它正在等待來自從未收到您的編碼命令的反饋,可能是無窮無盡...