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>
*
*/
class
Logger {
public
function
__construct(
/*Logging $logger*/
) {
}
public
function
logger(
$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 );
}
}
final
class
Signal{
public
static
$signo
= 0;
protected
static
$ini
= null;
public
static
function
set(
$signo
){
self::
$signo
=
$signo
;
}
public
static
function
get(){
return
(self::
$signo
);
}
public
static
function
reset(){
self::
$signo
= 0;
}
}
class
Test
extends
Logger {
//public static $signal = null;
public
function
__construct() {
//self::$signal == null;
}
public
function
run(){
while
(true){
pcntl_signal_dispatch();
printf(
"."
);
sleep(1);
if
(Signal::get() == SIGHUP){
Signal::reset();
break
;
}
}
printf(
"\n"
);
}
}
class
Daemon
extends
Logger {
/* config */
const
LISTEN =
"tcp://192.168.2.15:5555"
;
const
pidfile =
__CLASS__
;
const
uid = 80;
const
gid = 80;
const
sleep = 5;
protected
$pool
= NULL;
protected
$config
=
array
();
public
function
__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();
}
public
function
signal(){
pcntl_signal(SIGHUP,
function
(
$signo
)
/*use ()*/
{
//echo "\n This signal is called. [$signo] \n";
printf(
"The process has been reload.\n"
);
Signal::set(
$signo
);
});
}
private
function
daemon(){
if
(
file_exists
(
$this
->pidfile)) {
echo
"The file $this->pidfile exists.\n"
;
exit
();
}
$pid
= pcntl_fork();
if
(
$pid
== -1) {
die
(
'could not fork'
);
}
else
if
(
$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
());
}
}
private
function
run(){
while
(true){
printf(
"The process begin.\n"
);
$this
->
class
->run();
printf(
"The process end.\n"
);
}
}
private
function
foreground(){
$this
->run();
}
private
function
start(){
$pid
=
$this
->daemon();
for
(;;){
$this
->run();
sleep(self::sleep);
}
}
private
function
stop(){
if
(
file_exists
(
$this
->pidfile)) {
$pid
=
file_get_contents
(
$this
->pidfile);
posix_kill(
$pid
, 9);
unlink(
$this
->pidfile);
}
}
private
function
reload(){
if
(
file_exists
(
$this
->pidfile)) {
$pid
=
file_get_contents
(
$this
->pidfile);
//posix_kill(posix_getpid(), SIGHUP);
posix_kill(
$pid
, SIGHUP);
}
}
private
function
status(){
if
(
file_exists
(
$this
->pidfile)) {
$pid
=
file_get_contents
(
$this
->pidfile);
system(sprintf(
"ps ax | grep %s | grep -v grep"
,
$pid
));
}
}
private
function
help(
$proc
){
printf(
"%s start | stop | restart | status | foreground | help \n"
,
$proc
);
}
public
function
main(
$argv
){
if
(
count
(
$argv
) < 2){
$this
->help(
$argv
[0]);
printf(
"please input help parameter\n"
);
exit
();
}
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] ===
'foreground'
){
$this
->foreground();
}
else
if
(
$argv
[1] ===
'reload'
){
$this
->reload();
}
else
{
$this
->help(
$argv
[0]);
}
}
}
$daemon
=
new
Daemon(80,80,
new
Test());
$daemon
->main(
$argv
);
?>
这里例子实现start, stop,reload,status等等。
该文章转自OSChina
0 0
- PHP 写守护进程,实例分享
- 用PHP写一个Daemon守护进程
- php写守护进程(Daemon)
- php写守护进程(Daemon)
- php写守护进程(Daemon)
- php写守护进程(Daemon)
- php写守护进程(Daemon)
- PHP高级编程实例:编写守护进程
- 写守护进程daemon
- 怎样写守护进程?
- 守护进程实例
- linux 守护进程实例
- 守护进程 linux 实例
- 守护进程实例
- php守护进程
- PHP 做守护进程
- PHP 守护进程类
- php守护进程
- 设计概念澄清
- 黑马程序员——反射Constructor类
- ubuntu14全屏设置
- JAVA基础知识之冒泡排序
- 根据屏幕判断设备型号
- PHP 写守护进程,实例分享
- git查看某个文件的修改历史
- limits.conf介绍
- 扩展GridView控件——为内容项添加拖放及分组功能
- 开始iOS 7中自动布局教程(二)
- Oracle timestamp类型转换成date类型
- 关于PHP设计模式的分析和理解
- android:visible = "invisible"和android:visible = "gone"的区别
- OpenCV获取与设置像素点的值的几个方法