php实现简单的守护进程创建、开启与关闭操作
本文实例讲述了php实现简单的守护进程创建、开启与关闭操作。分享给大家供大家参考,具体如下:
前提要安装有pcntl扩展,可通过php-m查看是否安装
pidfile=dirname(__FILE__).'/daemontest.pid';
}
privatefunctionstartDeamon(){
if(file_exists($this->pidfile)){
echo"Thefile$this->pidfileexists.\n";
exit();
}
$pid=pcntl_fork();
if($pid==-1){
die('couldnotfork');
}elseif($pid){
echo'startok';
exit($pid);
}else{
//wearethechild
file_put_contents($this->pidfile,getmypid());
returngetmypid();
}
}
privatefunctionstart(){
$pid=$this->startDeamon();
while(true){
file_put_contents(dirname(__FILE__).'/test.txt',date('Y-m-dH:i:s'),FILE_APPEND);
sleep(2);
}
}
privatefunctionstop(){
if(file_exists($this->pidfile)){
$pid=file_get_contents($this->pidfile);
posix_kill($pid,9);
unlink($this->pidfile);
}
}
publicfunctionrun($argv){
if($argv[1]=='start'){
$this->start();
}elseif($argv[1]=='stop'){
$this->stop();
}else{
echo'paramerror';
}
}
}
$deamon=newDaemon();
$deamon->run($argv);
启动
phpdeamon.phpstart
关闭
phpdeamon.phpstop
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP进程与线程操作技巧总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。