page contents

php 守护进程(Daemon)

守护进程(daemon)是一类在后台运行的特殊进程,用于执行特定的系统任务。

attachments-2020-04-iMqwzfW45e8c39e058271.jpg

守护进程(daemon)是一类在后台运行的特殊进程,用于执行特定的系统任务。

很多守护进程在系统引导的时候启动,并且一直运行直到系统关闭。

守护进程一直在后台运行,脱离终端运行的程序 独立运行的守护进程。

<?php。
class myProcess
{
    const UID = 80;
    const GID = 80;
    protected $loop = true;
    protected $pidFile = "/tmp/myprocess.pid";
    //protected $data = "123132132\r\n";

    private function checkPidFile()
    {
        if(file_exists($this->pidFile))
        {
            echo "daemon process is already runing.\n";
            exit();
        }
    }
    private function daemon()
    {
    
        $pid = pcntl_fork();
        if ($pid == -1)
        {
            exit("fork fail");
        }else if($pid == 0){
            $this->handler();
        }else{
            exit();
        }
    }

    private function handler()
    {
        posix_setsid();
        posix_setuid(self::UID);
        posix_setgid(self::GID);
        #fclose(STDIN);  
        #fclose(STDOUT);  
        #fclose(STDERR); 
        #$pid = posix_getpid
        $pid = getmypid();
        file_put_contents($this->pidFile, $pid); #or die("open file fail");
    }


    private function start()
    {
        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");
            sleep(1);
        }
    }

    private function stop()
    {
        if (file_exists($this->pidFile)) {
           $pid = file_get_contents($this->pidFile);
           posix_kill($pid, SIGHUP);
           unlink($this->pidFile);
           echo "stop success.....";
        }else{
            echo "stop fail, daemon process not is already runing";
        }
    }

    private function restart()
    {
        $this->stop();
        $this->start();    
    }

    public function reload()
    {
        // $this->loop = false;
        // pcntl_signal_dispatch();
        // $this->loop = true;
        // $this->start();
    }

    public function main($argv)
    {
        switch ($argv[1]) {
             case 'start':
               $this->start();
               break;
             case 'stop':
                  $this->stop();
                  break;
            case 'restart':
                  $this->restart();
                  break;
            case 'reload':
                  $this->reload();
                  break;
             default: 
                 echo 'php process.php start | stop | restart | reload';
                 exit;
        }
    }

}

$proce = new myProcess();
$proce->main($argv);



attachments-2020-04-eGjbAZF95e8c39f51c739.jpg

  • 发表于 2020-04-07 16:29
  • 阅读 ( 352 )
  • 分类:PHP开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1135 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章