php进程管理

<?php
    /**
     * Created by PhpStorm.
     * User: 
     * Date: 2016/7/15
     * Time: 16:38
     */


class ChildWork{


    function  run(){
        while(true){
            sleep(5);
            file_put_contents('work.log',date("y-m-d H:i:s" ,time()) ."\n" ,FILE_APPEND) ;
        }
    }

}

class work{


    public  $pidNum = 1;  // 开放进程数目
    public  $path = __DIR__ ; //
    public  $chlidWork = Null;
    public  $pidFileList = [];



    public function __construct($pidNum , $childInstance){

        $this->pidNum  = $pidNum ;
        $this->chlidWork = $childInstance ;
        $this->path = $this->path .'/pid';
        if(!file_exists($this->path)){
            mkdir($this->path,0777);
        }

    }


    /**
     * @author  
     */
    function start(){
        if($this->pidNum > 1) {
            for($i = 0; $i < $this->pidNum; ++$i) {
                $pid = $this->fork();
                if($pid == -1) {
                    die();
                }
                // Child, start the worker
                else if(!$pid) {
                    $this->chlidWork->run();
                }
            }
        }

    }

    /**
     * @author  
     */
    function background(){
        $sid = posix_setsid();//设置新会话组长,脱离终端
        echo 'childen *****' .posix_getppid()."\n";
        if ($sid < 0)
            exit;
    }


    /**
     * 守护进程
     * @author  
     */
    function daemon(){
        if($this->pidNum > 1) {
            for($i = 0; $i < $this->pidNum; ++$i) {
                $pid = $this->fork();
                if($pid == -1) {
                    die();
                }
                // Child, start the worker
                else if(!$pid) {
                    $this->pidFileList[$i] = $this->path.'/'.getmypid().'.pid';
                    file_put_contents ($this->pidFileList[$i], getmypid());
                    $sid = posix_setsid();//设置新会话组长,脱离终端
                    file_put_contents('daemon.log',json_encode($this->pidFileList));
                    $this->chlidWork->run();
                }

            }
        }


    }



    /**
     * fork 子进程
     * @return int
     * @author  
     */
    function fork(){
        if(!function_exists('pcntl_fork')) {
            return -1;
        }
        $pid = pcntl_fork();
        if($pid === -1) {
           exit;
        }
        return $pid;
    }


    function stop(){


        $dir = $this->path;
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    if($file =='.' || $file =='..')
                        continue;
                    $pidFile=  $this->path.'/'.$file ;
                    $pid =   file_get_contents($pidFile);
                    posix_kill($pid, 9);
                    unlink($pidFile);
                } closedir($dh);
            }
        }


    }

    private function help($proc){
        printf("%s start | stop | help \n", $proc);
    }





    function main($argv){
        if($argv[1] === 'stop'){
            $this->stop();
        }else if($argv[1] === 'start'){
            $this->start();
        }else if($argv[1] === 'restart'){
            $this->stop();
            $this->start();
        }else if($argv[1] === 'status'){
//            $this->status();
        }else if($argv[1] === 'daemon'){
            $this->daemon();
        }else if($argv[1] === 'reload'){
//            $this->reload();
        }else{
            $this->help($argv[0]);
        }
    }



}


    $work = new work(2,new ChildWork());
    $work->main($argv);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值