2014-10-08 106 views
3

我在PHP中有一個腳本,它使用Curl逐個打擊一長串URL並在最後將響應寫入文件,我希望從列表中上傳列表一個瀏覽器,並在後臺執行其餘的處理(而不會讓用戶等待響應)。我已經嘗試了以下解決方案 -在Windows機器上長時間運行PHP的後臺進程

$command = "C:\wamp\bin\php\php5.5.12\php.exe ../background_process/subscribe_bg.php ".$file_temp_path; 
    shell_exec(sprintf('%s > /dev/null 2>/dev/null &', $command)); 

這會成功運行腳本,但會使瀏覽器等待。 (這可能會在Linux機器上的後臺運行。)

$command = "C:\wamp\bin\php\php5.5.12\php.exe ../background_process/subscribe_bg.php ".$file_temp_path; 

    execInBackground($command); 

    function execInBackground($cmd) { 
     if (substr(php_uname(), 0, 7) == "Windows"){ 
      pclose(popen("start /B ". $cmd, "r")); 
     } 
     else { 
      exec($cmd . " > /dev/null &"); 
     } 
    } 

我發現這個解決方案的Windows機器,但對於me.The腳本不起作用根本不會執行。

請在Windows機器上使用PHP在後臺建議運行一個很長的過程(不是很長〜30-40分鐘)的最佳實踐。

+0

謝謝你,sainiankit。這行:pclose(popen(「start/B」。$ cmd,「r」));爲我工作。我在Windows 7上運行PHP 5.3。 – kdanylo 2015-09-13 13:04:35

+0

'腳本根本不執行。' - 實際上,只要父進程(http請求)執行就會死掉。一個[後臺工作實施](https://www.google.com/search?q=php+background+job+queue&oq=php+background+job+queue)(或在捏[forking](http:// php.net/manual/en/function.pcntl-fork.php))太寬泛,無法在Stackoverflow上描述 - 但這就是你要求的。 – AD7six 2015-09-13 13:14:57

回答

2

這完全Works的Windows機器上,與寫入$ response_file_path腳本的所有輸出 -

$command = $PHP_DIR." ../background_process/subscribe_bg.php -p=".$file_path_arg." >../sublogs/responses/".$file_response_path." 2>../sublogs/error_logs/err.txt"; 

execInBackground($command) 

function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
     pclose(popen("start /B ". $cmd, "r")); 
     return 1; 
    } 
    else { 
     return 0; 
     //Or the code for linux machine. 
    } 
} 
+0

正是我的意思。它之前沒有工作,因爲你的初始命令不是將輸出重定向到一個文件)))謝謝。 – kdanylo 2015-09-16 01:55:37

+0

哦,是的!確切地說,這使得瀏覽器等待該過程完成。 – sainiankit 2015-09-16 05:17:52

0

我認爲你應該將你的後臺進程的輸出重定向到另一個文件。既然你沒有重定向它,PHP正在等待它。添加'>'重定向到你的命令:

$command = "C:\wamp\bin\php\php5.5.12\php.exe ../background_process/subscribe_bg.php ".$file_temp_path . " > out.log "; 

祝你好運! Danylo

相關問題