博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fork+exit+php,php实现简单的守护进程创建、开启与关闭操作
阅读量:7025 次
发布时间:2019-06-28

本文共 1252 字,大约阅读时间需要 4 分钟。

本文实例讲述了php实现简单的守护进程创建、开启与关闭操作。分享给大家供大家参考,具体如下:

前提要安装有pcntl扩展,可通过php -m查看是否安装

class Daemon {

private $pidfile;

function __construct() {

$this->pidfile = dirname(__FILE__).'/daemontest.pid';

}

private function startDeamon() {

if (file_exists($this->pidfile)) {

echo "The file $this->pidfile exists.\n";

exit();

}

$pid = pcntl_fork();

if ($pid == -1) {

die('could not fork');

} else if ($pid) {

echo 'start ok';

exit($pid);

} else {

// we are the child

file_put_contents($this->pidfile, getmypid());

return getmypid();

}

}

private function start(){

$pid = $this->startDeamon();

while (true) {

file_put_contents(dirname(__FILE__).'/test.txt', date('Y-m-d H:i:s'), FILE_APPEND);

sleep(2);

}

}

private function stop(){

if (file_exists($this->pidfile)) {

$pid = file_get_contents($this->pidfile);

posix_kill($pid, 9);

unlink($this->pidfile);

}

}

public function run($argv) {

if($argv[1] == 'start') {

$this->start();

}else if($argv[1] == 'stop') {

$this->stop();

}else{

echo 'param error';

}

}

}

$deamon = new Daemon();

$deamon->run($argv);

启动

php deamon.php start

关闭

php deamon.php stop

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP进程与线程操作技巧总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

转载地址:http://jksxl.baihongyu.com/

你可能感兴趣的文章
IPv6系列-初学者的10个常见困扰
查看>>
通过QEMU-GuestAgent实现从外部注入写文件到KVM虚拟机内部
查看>>
linux ip配置
查看>>
【Android开发】如何实现android和服务器长连接呢?推送消息的原理
查看>>
关于securecrt7.2版本安装在win8系统激活的问题
查看>>
我的友情链接
查看>>
python 路径问题
查看>>
flink could not find implicit value for evidence parameter of type
查看>>
Centos6.3 下apache-solr-3.6.2安装和配置
查看>>
Windows程序中的字符编码问题
查看>>
Redhat安装hba卡,配置emc powerpath,配置LVM
查看>>
html 的最简洁的遮罩
查看>>
我的友情链接
查看>>
走出问题的乌托邦
查看>>
今天的工作日志
查看>>
Opensuse12.2配置 Apache2+PHP5+MySQL(LAMP)
查看>>
经典SQL语法大全
查看>>
使用数据库统一管理ssh登陆用户密钥信息
查看>>
Active 与 Service 的生命周期、保存数据----Day04 2014.5.29
查看>>
ip6tables 基本配置
查看>>