-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHPDaemon.php
executable file
·135 lines (99 loc) · 2.68 KB
/
PHPDaemon.php
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
<?php
class Log {
public $prefix;
function Log ( $facility=LOG_LOCAL4 , $ident = __CLASS__ , $option = FALSE ) {{{
if(empty($option)) {
$option = (LOG_PID|LOG_ODELAY);
}
$this->prefix = '';
return openlog( $ident , $option , $facility );
}}}
function syslog($msg , $level = FALSE ) {{{
if(empty($level)) {
$priority = LOG_INFO;
} else {
$priority = (int)$level;
}
$len = strlen ($msg);
$p = 0; // Linux has a length of 500 characters wrap the string
while ( $p < $len ) {
$str = substr($msg,$p,'480');
$p = $p + '480';
syslog ($priority , $this->prefix.$str );
}
}}}
function close () {{{
return closelog();
}}}
}
class PHPDaemon {
public $log;
public $sleep;
private $daemonName;
function signal_handler($signo) {{{
switch ($signo) {
case SIGTERM:
$this->log->syslog("Stopping $this->daemonName Process");
$this->log->close();
unlink("/var/run/$this->daemonName.pid");
exit;
break;
default:
// handle all other signals
}
}}}
function init_tasks() {{{
if (posix_setsid() === -1) {
die('Could not setsid');
}
fclose(STDIN); // Close all of the standard
fclose(STDOUT); // file descriptors as we
fclose(STDERR); // are running as a daemon.
set_time_limit (0);
chdir("/");
// setup signal handlers
foreach(array(SIGTERM, SIGINT, SIGUSR1, SIGHUP, SIGCHLD) as $signal)
pcntl_signal($signal, array($this, 'signal_handler'));
}}}
function init( $daemonName = 'daemon' ) {{{
if(file_exists("/var/run/$daemonName.pid")) {
echo("$daemonName is already running!\n");
exit;
}
$this->daemonName = $daemonName;
// Setup log class
$this->log = new Log(LOG_LOCAL5 , $this->daemonName);
$pid = pcntl_fork();
switch($pid) {
case -1:
return 1;
break;
case 0:
$this->childPid = getmypid();
$this->log->syslog("Forked - child process ($this->childPid)");
$this->init_tasks();
$this->sleep = 1; // Default to 1 second
break;
default:
file_put_contents("/var/run/$this->daemonName.pid",$pid);
exit;
}
return;
}}}
function run( $function ) {{{
// loop forever
for (;;) {
pcntl_signal_dispatch(); // remember to call the signal dispatch to see if we have any waiting signals
// Check to see if its a function call
if (is_string($function) && function_exists($function)) {
call_user_func_array($function,array(&$this->log));
}
// Check to see if its an anonymous function
else if(is_object($function) && ($function instanceof Closure)) {
$function($this->log); // Function we wish to call
}
sleep($this->sleep);
}
}}}
}
?>