守护进程(daemon)是一个特殊的进程,它运行在后台,执行特定的系统任务。许多守护进程在系统启动时启动,并一直运行到系统关闭。守护进程一直在后台运行,一个独立于终端上运行的程序运行的守护进程。pidFile)){echo"守护进程已经运行。\n";出口();}}privatefunctiondaemon(){$pid=pcntl_fork();如果($pid==-1){退出(“fork失败”);}elseif($pid==0){$this->handler();}else{退出();}}privatefunctionhandler(){posix_setsid();posix_setuid(self::UID);posix_setgid(self::GID);#fclose(标准输入);#fclose(标准输出);#fclose(STDERR);#$pid=posix_getpid$pid=getmypid();file_put_contents($this->pidFile,$pid);#ordie("打开文件失败");}私有函数开始(){pcntl_signal_dispatch();$this->checkPidFile();$this->daemon();while($this->loop){$id=getmypid();//$f=fopen("/tmp/a.txt",a);//fwrite($f,$this->data);//file_put_contents("/tmp/".$id.".log","进程ID:$id正常运行\r\n");睡觉(1);}}privatefunctionstop(){if(file_exists($this->pidFile)){$pid=file_get_contents($this->pidFile);posix_kill($pid,SIGHUP);取消链接($this->pidFile);echo"停止成功.....";}else{echo"stopfail,daemonprocessnotalreadyrunning";}}privatefunctionrestart(){$this->stop();$this->开始();}publicfunctionreload(){//$this->loop=false;//pcntl_signal_dispatch();//$this->loop=true;//$this->start();}公共函数main($argv){switch($argv[1]){case'start':$this->start();休息;案例“停止”:$this->stop();休息;case'restart':$this->restart();休息;case'reload':$this->reload();休息;默认值:echo'phpprocess.phpstart|停止|重启|重新加载';出口;}}}$proce=newmyProcess();$proce->main($argv);
