2011-01-24 109 views
0

我有一個PHP腳本作爲守護進程運行。每隔一段時間,我都希望腳本休息一下並重新啓動,以便在收到信號時清除內存使用情況。PHP守護進程自動重啓失敗

我一直在看shell_exec();殺死並重新啓動PHP腳本,但我想知道是否有更清潔的方法。我也研究過在一批中包裝PHP腳本,並在需要時重新啓動它,但我只在PHP中有知識。

declare(ticks = 1); 
    $processID = pcntl_fork(); 
    if ($processID == -1) { 
     echo "\n Error: The process failed to fork. \n"; 
    } else if ($processID) { 
     exit; 
    } else { 
    } 
    if (posix_setsid() == -1) { 
     echo "\n Error: Unable to detach from the terminal window. \n"; 
    } 
    $posixProcessID = posix_getpid(); 
    $filePointer = fopen("/var/run/addy.pid" , "w"); 
    fwrite($filePointer , $posixProcessID); 
    fclose($filePointer); 


gc_enable(); 
while (true) { 

sleep(1); 

print "debug: memory_get_peak_usage: ".memory_get_peak_usage()." debug: memory_get_usage: ".memory_get_usage()."\n"; 

    // STUFF GOES HERE 

unset($array); 
gc_collect_cycles(); 
    } 

謝謝你的幫忙!

回答

3

一種方法是使用BASH腳本來運行守護進程。

#!/bin/bash 

while [ 1 ]; do 
    ./my_php_daemon 
done 

然後只要你想讓它重新啓動就退出你的php守護進程。

+0

我喜歡這是唯一的答案,它很簡單,並解決了問題,它得到-1沒有評論或替代建議... – gravitron 2011-01-26 15:37:10