天天看点

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); 最后将该文件删除。