《PHP編程:PHP創(chuàng)建單例后臺進程的方法示例》要點:
本文介紹了PHP編程:PHP創(chuàng)建單例后臺進程的方法示例,希望對您有用。如果有疑問,可以聯(lián)系我們。
本文實例講述了PHP創(chuàng)建單例后臺進程的方法.分享給大家供大家參考,具體如下:PHP學習
可以通過如下語句啟動一個PHP后臺進程:PHP學習
$command = " php script.php "; $pid = exec("nohup $command > /dev/null 2>&1 & echo $!");
nohup表示這個進程獨立于創(chuàng)建的用戶,可以以守護方式運行.PHP學習
如果需要這個后臺進程是單例運行的,那么可以通過下面的方法來記錄/判斷進程是否已運行PHP學習
//query the database for process id $query = "SELECT pid FROM `daemons` WHERE `pid` = '2013' LIMIT 1"; $result = mysql_query($query); $pid = mysql_result($result, 0, 'pid'); //check if the process is running exec("ps $pid", $pState); if((count($pState) >= 2) && !empty($pid)) { echo "RUNNING"; } else { echo "INACTIVE"; }
也可以把pid寫入文件,但如果在一個分布式任務環(huán)境中,則放在數(shù)據(jù)庫中要更好PHP學習
停止一個后臺進程:PHP學習
//check if the process from the database is running exec("ps $pid", $pState); if((count($pState) >= 2)) { //if the process is running, kill it exec("kill $pid"); //update database row with an empty process id }
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP進程與線程操作技巧總結(jié)》、《PHP網(wǎng)絡編程技巧總結(jié)》、《PHP基本語法入門教程》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》PHP學習
希望本文所述對大家PHP程序設計有所幫助.PHP學習
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/751.html