天天看點

PHP進階程式設計之守護程序

中國廣東省深圳市龍華新區民治街道溪山美地

518131

+86 13113668890

+86 755 29812080

版權聲明

轉載請與作者聯系,轉載時請務必标明文章原始出處和作者資訊及本聲明。

PHP進階程式設計之守護程式

文檔出處:

<a href="http://netkiller.github.io/" target="_top">http://netkiller.github.io</a>

<a href="http://netkiller.sourceforge.net/" target="_top">http://netkiller.sourceforge.net</a>

2014-09-01

摘要

我的系列文檔

<a href="http://netkiller.github.io/architect/index.html" target="_top">netkiller architect 手劄</a>

<a href="http://netkiller.github.io/developer/index.html" target="_top">netkiller developer 手劄</a>

<a href="http://netkiller.github.io/php/index.html" target="_top">netkiller php 手劄</a>

<a href="http://netkiller.github.io/python/index.html" target="_top">netkiller python 手劄</a>

<a href="http://netkiller.github.io/testing/index.html" target="_top">netkiller testing 手劄</a>

<a href="http://netkiller.github.io/cryptography/index.html" target="_top">netkiller cryptography 手劄</a>

<a href="http://netkiller.github.io/linux/index.html" target="_top">netkiller linux 手劄</a>

<a href="http://netkiller.github.io/debian/index.html" target="_top">netkiller debian 手劄</a>

<a href="http://netkiller.github.io/centos/index.html" target="_top">netkiller centos 手劄</a>

<a href="http://netkiller.github.io/freebsd/index.html" target="_top">netkiller freebsd 手劄</a>

<a href="http://netkiller.github.io/shell/index.html" target="_top">netkiller shell 手劄</a>

<a href="http://netkiller.github.io/security/index.html" target="_top">netkiller security 手劄</a>

<a href="http://netkiller.github.io/www/index.html" target="_top">netkiller web 手劄</a>

<a href="http://netkiller.github.io/monitoring/index.html" target="_top">netkiller monitoring 手劄</a>

<a href="http://netkiller.github.io/storage/index.html" target="_top">netkiller storage 手劄</a>

<a href="http://netkiller.github.io/mail/index.html" target="_top">netkiller mail 手劄</a>

<a href="http://netkiller.github.io/docbook/index.html" target="_top">netkiller docbook 手劄</a>

<a href="http://netkiller.github.io/version/index.html" target="_top">netkiller version 手劄</a>

<a href="http://netkiller.github.io/database/index.html" target="_top">netkiller database 手劄</a>

<a href="http://netkiller.github.io/postgresql/index.html" target="_top">netkiller postgresql 手劄</a>

<a href="http://netkiller.github.io/mysql/index.html" target="_top">netkiller mysql 手劄</a>

<a href="http://netkiller.github.io/nosql/index.html" target="_top">netkiller nosql 手劄</a>

<a href="http://netkiller.github.io/ldap/index.html" target="_top">netkiller ldap 手劄</a>

<a href="http://netkiller.github.io/network/index.html" target="_top">netkiller network 手劄</a>

<a href="http://netkiller.github.io/cisco/index.html" target="_top">netkiller cisco ios 手劄</a>

<a href="http://netkiller.github.io/h3c/index.html" target="_top">netkiller h3c 手劄</a>

<a href="http://netkiller.github.io/multimedia/index.html" target="_top">netkiller multimedia 手劄</a>

<a href="http://netkiller.github.io/perl/index.html" target="_top">netkiller perl 手劄</a>

<a href="http://netkiller.github.io/radio/index.html" target="_top">netkiller amateur radio 手劄</a>

<a href="http://netkiller.github.io/devops/index.html" target="_top">netkiller devops 手劄</a>

目錄

<a href="http://netkiller.github.io/journal/php.daemon.html#what">1. 什麼是守護程序</a>

<a href="http://netkiller.github.io/journal/php.daemon.html#why">2. 為什麼開發守護程序</a>

<a href="http://netkiller.github.io/journal/php.daemon.html#when">3. 何時采用守護程序開發應用程式</a>

<a href="http://netkiller.github.io/journal/php.daemon.html#security">4. 守護程序的安全問題</a>

<a href="http://netkiller.github.io/journal/php.daemon.html#how">5. 怎樣開發守護程序</a>

<a href="http://netkiller.github.io/journal/php.daemon.html#idp59045696">5.1. 程式啟動</a>

<a href="http://netkiller.github.io/journal/php.daemon.html#idp59049184">5.2. 程式停止</a>

守護程序是脫離于終端并且在背景運作的程序。守護程序脫離于終端是為了避免程序在執行過程中的資訊在任何終端上顯示并且程序也不會被任何終端所産生的終端資訊所打斷。

很多程式以服務形式存在,他沒有終端或ui互動,它可能采用其他方式與其他程式互動,如tcp/udp socket, unix socket, fifo。程式一旦啟動便進入背景,直到滿足條件他便開始處理任務。

如果我不采用守護程序方式開發該程式,程式一旦運作就會占用目前終端窗框,還有受到目前終端鍵盤輸入影響,有可能程式誤退出。

我們希望程式在非超級使用者運作,這樣一旦由于程式出現漏洞被駭客控制,攻擊者隻能繼承運作權限,而無法獲得超級使用者權限。

我們希望程式隻能運作一個執行個體,不運作同僚開啟兩個以上的程式,因為會出現端口沖突等等問題。

例 1. 守護程序例示

下面是程式啟動後進入背景的代碼

通過程序id檔案來判斷,目前程序狀态,如果程序id檔案存在表示程式在運作中,通過代碼file_exists($this-&gt;pidfile)實作,但而後程序被kill需要手工删除該檔案才能運作

程式啟動後,父程序會推出,子程序會在背景運作,子程序權限從root切換到指定使用者,同時将pid寫入程序id檔案。

程式停止,隻需讀取pid檔案,然後調用posix_kill($pid, 9); 最後将該檔案删除。