2010-08-06 20 views
0

我在這方面花了一段時間,但不能得到這個工作,我很抱歉,因爲我早前提出了一個與此有關的問題,但刪除了它,所以我可以做更多的研究來縮小這個問題,在這一點上,我我堅持,因爲我認爲我有這個解決方案,但它不工作,因爲我期望..我找到了一個論壇,有人問了類似的問題,他們給了像下面的代碼,我試着..它確實運行腳本,但它仍然等待其纔去到下一行的PHP代碼PHP使用proc_open,以便它不會等待它打開(運行)完成的腳本?

proc_close(proc_open ("/var/www/other_scripts/perl/apps/emails_extract_from_url.pl \"$stoopid\"", array(), $foo)); 

回答

0

我的一些代碼,當我與proc_open
發揮各地我曾與proc_close(10〜30秒),所以我只是用殺進程的問題linux命令殺人



$options=array(); 
$option['sleep-after-destroy']=0; 
$option['sleep-after-create']=0; 
$option['age-max']=40; 
$option['dir-run']=dirname(__FILE__); 
$option['step-sleep']=1; 
$option['workers-max']=(int)file_get_contents($maxworkers_file); 
$option['destroy-forcefull']=1; 

$workers=array(); 

function endAWorker($i,$cansleep=true) { 
     global $workers; 
     global $option; 
     global $child_time_limit; 
     if(isset($workers[$i])) { 
       @doE('Ending worker [['.$i.']]'."\n"); 
       if($option['destroy-forcefull']==1) { 
         $x=exec('ps x | grep "php check_working_child.php '.$i.' '.$child_time_limit.'" | grep -v "grep" | grep -v "sh -c"'); 
         echo 'pscomm> '.$x."\n"; 
         $x=explode(' ',trim(str_replace("\t",' ',$x))); 
         //print_r($x); 
         if(is_numeric($x[0])) { 
           $c='kill -9 '.$x[0]; 
           echo 'killcommand> '.$c."\n"; 
           $x=exec($c); 
         } 
       } 
       @proc_close($workers[$i]['link']); 
       unset($workers[$i]); 
     } 
     if($cansleep==true) { 
       sleep($option['sleep-after-destroy']); 
     } 
} 

function startAWorker($i) { 
     global $workers; 
     global $option; 
     global $child_time_limit; 

     $runcommand='php check_working_child.php '.$i.' '.$child_time_limit.' > check_working_child_logs/'.$i.'.normal.log'; 
     doE('Starting [['.$i.']]: '.$runcommand."\n"); 
     $workers[$i]=array(
       'desc' => array(
         0 => array("pipe", "r"), 
         1 => array("pipe", "w"), 
         2 => array("file", 'check_working_child_logs/'.$i.'.error.log', "a") 
         ), 
       'pipes'     => null, 
       'link'     => null, 
       'start-time' => mktime() 
       ); 
     $workers[$i]['link']=proc_open(
       $runcommand, 
       $workers[$i]['desc'], 
       $workers[$i]['pipes'], 
       $option['dir-run'] 
       ); 
     sleep($option['sleep-after-create']); 
} 

function checkAWorker($i) { 
     global $workers; 
     global $option; 
     $temp=proc_get_status($workers[$i]['link']); 
     if($temp['running']===false) { 
       doE('Worker [['.$i.']] finished'."\n"); 
       if(is_file('check_working_child_logs/'.$i.'.normal.log') && filesize('check_working_child_logs/'.$i.'.normal.log')>0) { 
         doE('--------'."\n"); 
         echo file_get_contents('check_working_child_logs/'.$i.'.normal.log'); 
         doE('-------'."\n"); 
       } 
       endAWorker($i); 
     } else { 
       if($option['age-max']>0) { 
         if($workers[$i]['start-time']+$option['age-max']$v) { 
       endAWorker($i,false); 
     } 
     @doE('Done killing workers.'."\n"); 
} 

register_shutdown_function('endAllWorkers'); 

while(1) { 
     $step++; 
     foreach($workers as $index=>$v) { 
       checkAWorker($index); 
     } 
     if(count($workers)==$option['workers-max']) { 
     } elseif(count($workers)$option['workers-max']) { 
       $wl=array_keys($workers); 
       $wl=array_pop($wl); 
       doE('Killing worker [['.$wl.']]'); 
       endAWorker($wl[0]); 
     } 
} 

+0

是的,我有同樣的問題是扔我離開,因爲我的PHP腳本實際上並沒有等待,我只是以爲這是因爲本應該完成的Perl腳本仍在運行......似乎在做一個命令行殺是一個很好的路線 – Rick 2010-08-07 21:30:15

+0

doe()實際上是一個回聲,忘記了功能。 – 2010-08-08 00:56:33

3

嘿,我一直在使用proc_open很多最近,它從不等待其在移動之前完成完成。確保你指定的管道,而不是隻使用一個空的數組()

$descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from 
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to 
    2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to 
); 

並且還將proc_open分配給一個變量。

$myProc = proc_open("/var/www/other_scripts/perl/apps/emails_extract_from_url.pl \"$stoopid\"", $descriptorspec , $foo) 

然後,您可以通過使用proc_get_status($myProc);

更多的信息在這裏http://au.php.net/proc_open

更多信息收得到您的進程的狀態。

$temp = fgets($this->open_pipes[$thread_id][1], 1024); 
if($this->checkFinishedThread($thread_id)) 
{ 
    fclose($this->open_pipes[$thread_id][1]); 
    proc_close($thread); 
} 

function checkFinishedThread($thread_id) 
{ 
    $test = stream_get_meta_data($this->open_pipes[$thread_id][1]); 
    if($test['eof'] == 1) 
     return true; 
    return false; 
} 
+0

謝謝,將檢查到..我只是通過一個帖子,那傢伙說這樣做,它應該工作,所以我會修改它,因爲你建議 – Rick 2010-08-06 04:23:34

+0

更新:我已經嘗試過你發佈的,但它仍然在等待它所調用的腳本完成,我發現這個奇怪的..我使用PHP與Apache(所以我從Web瀏覽器調用它),所以我不知道這是否會影響它不知何故 – Rick 2010-08-06 04:32:26

+0

嗯,它可能。我通常在命令行上運行時使用它。但在說,我確定我已經在瀏覽器上工作過,並且它已經工作了。你可以在你的文章中發佈整個劇本嗎? – ozatomic 2010-08-06 04:49:30

0

在命令行參數末尾需要一個非線性(&):

proc_close(proc_open ("/var/www/other_scripts/perl/apps/emails_extract_from_url.pl \"$stoopid\" & ", array(), $foo)); 
相關問題