PHP 写守护进程,实例分享

来源:互联网 发布:淘宝客推广软文范例 编辑:程序博客网 时间:2024/06/17 22:42

http://netkiller.github.io/journal/php.daemon.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/*
 * PHP Daemon sample.
 * Home: http://netkiller.github.io
 * Author: netkiller<netkiller@msn.com>
 *
*/
classLogger {
     
    publicfunction__construct(/*Logging $logger*/) {
    }
 
    publicfunctionlogger($type,$message) {
        $log= sprintf ( "%s\t%s\t%s\n",date('Y-m-d H:i:s' ), $type,$message);
        file_put_contents( sprintf(__DIR__."/../log/sender.%s.log",date('Y-m-d')),$log, FILE_APPEND );
    }
     
}
 
finalclassSignal{
    publicstatic$signo = 0;
    protectedstatic$ini = null;
    publicstaticfunction set($signo){
        self::$signo=$signo;
    }
    publicstaticfunction get(){
        return(self::$signo);
    }
    publicstaticfunction reset(){
        self::$signo= 0;
    }
}
 
classTestextendsLogger {
    //public static $signal = null;
     
    publicfunction__construct() {
        //self::$signal == null;
    }
    publicfunctionrun(){
        while(true){
            pcntl_signal_dispatch();
            printf(".");
            sleep(1);
            if(Signal::get() == SIGHUP){
                Signal::reset();
                break;
            }
        }
        printf("\n");
    }
}
 
classDaemonextendsLogger {
    /* config */
    constLISTEN = "tcp://192.168.2.15:5555";
    constpidfile   = __CLASS__;
    constuid       = 80;
    constgid       = 80;
    constsleep = 5;
 
    protected$pool    = NULL;
    protected$config  = array();
 
    publicfunction__construct($uid,$gid,$class) {
        $this->pidfile = '/var/run/'.basename(get_class($class),'.php').'.pid';
        //$this->config = parse_ini_file('sender.ini', true); //include_once(__DIR__."/config.php");
        $this->uid = $uid;
        $this->gid = $gid;
        $this->class=$class;
        $this->classname = get_class($class);
         
        $this->signal();
    }
    publicfunctionsignal(){
 
        pcntl_signal(SIGHUP, function($signo)/*use ()*/{
            //echo "\n This signal is called. [$signo] \n";
            printf("The process has been reload.\n");
            Signal::set($signo);
        });
 
    }
    privatefunctiondaemon(){
        if(file_exists($this->pidfile)) {
            echo"The file $this->pidfile exists.\n";
            exit();
        }
 
        $pid= pcntl_fork();
        if($pid== -1) {
             die('could not fork');
        }elseif($pid) {
             // we are the parent
             //pcntl_wait($status); //Protect against Zombie children
            exit($pid);
        }else{
            file_put_contents($this->pidfile,getmypid());
            posix_setuid(self::uid);
            posix_setgid(self::gid);
            return(getmypid());
        }
    }
    privatefunctionrun(){
 
        while(true){
             
            printf("The process begin.\n");
            $this->class->run();
            printf("The process end.\n");
             
        }
    }
    privatefunctionforeground(){
        $this->run();
    }
    privatefunctionstart(){
        $pid=$this->daemon();
        for(;;){
            $this->run();
            sleep(self::sleep);
        }
    }
    privatefunctionstop(){
 
        if(file_exists($this->pidfile)) {
            $pid=file_get_contents($this->pidfile);
            posix_kill($pid, 9);
            unlink($this->pidfile);
        }
    }
    privatefunctionreload(){
        if(file_exists($this->pidfile)) {
            $pid=file_get_contents($this->pidfile);
            //posix_kill(posix_getpid(), SIGHUP);
            posix_kill($pid, SIGHUP);
        }
    }  
    privatefunctionstatus(){
        if(file_exists($this->pidfile)) {
            $pid=file_get_contents($this->pidfile);
            system(sprintf("ps ax | grep %s | grep -v grep",$pid));
        }
    }
    privatefunctionhelp($proc){
        printf("%s start | stop | restart | status | foreground | help \n",$proc);
    }
    publicfunctionmain($argv){
 
        if(count($argv) < 2){
            $this->help($argv[0]);
            printf("please input help parameter\n");
            exit();
        }
        if($argv[1] === 'stop'){
            $this->stop();
        }elseif($argv[1] === 'start'){
            $this->start();
        }elseif($argv[1] === 'restart'){
            $this->stop();
            $this->start();
        }elseif($argv[1] === 'status'){
            $this->status();
        }elseif($argv[1] === 'foreground'){
            $this->foreground();
        }elseif($argv[1] === 'reload'){
            $this->reload();
        }else{
            $this->help($argv[0]);
        }
    }
}
 
$daemon=newDaemon(80,80,newTest());
$daemon->main($argv);
?>



这里例子实现start, stop,reload,status等等。

该文章转自OSChina

0 0
原创粉丝点击