| 1 |
lars |
1 |
#!/usr/bin/php -q
|
|
|
2 |
<?php
|
|
|
3 |
/**
|
|
|
4 |
* System_Daemon turns PHP-CLI scripts into daemons.
|
|
|
5 |
*
|
|
|
6 |
* PHP version 5
|
|
|
7 |
*
|
|
|
8 |
* @category System
|
|
|
9 |
* @package System_Daemon
|
|
|
10 |
* @author Kevin <kevin@vanzonneveld.net>
|
|
|
11 |
* @copyright 2008 Kevin van Zonneveld
|
|
|
12 |
* @license http://www.opensource.org/licenses/bsd-license.php
|
|
|
13 |
* @link http://github.com/kvz/system_daemon
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* System_Daemon Example Code
|
|
|
18 |
*
|
|
|
19 |
* If you run this code successfully, a daemon will be spawned
|
|
|
20 |
* but unless have already generated the init.d script, you have
|
|
|
21 |
* no real way of killing it yet.
|
|
|
22 |
*
|
|
|
23 |
* In this case wait 3 runs, which is the maximum for this example.
|
|
|
24 |
*
|
|
|
25 |
*
|
|
|
26 |
* In panic situations, you can always kill you daemon by typing
|
|
|
27 |
*
|
|
|
28 |
* killall -9 logparser.php
|
|
|
29 |
* OR:
|
|
|
30 |
* killall -9 php
|
|
|
31 |
*
|
|
|
32 |
*/
|
|
|
33 |
|
|
|
34 |
// Allowed arguments & their defaults
|
|
|
35 |
$runmode = array(
|
|
|
36 |
'no-daemon' => false,
|
|
|
37 |
'help' => false,
|
|
|
38 |
'write-initd' => false,
|
|
|
39 |
);
|
|
|
40 |
|
|
|
41 |
// Scan command line attributes for allowed arguments
|
|
|
42 |
foreach ($argv as $k=>$arg) {
|
|
|
43 |
if (substr($arg, 0, 2) == '--' && isset($runmode[substr($arg, 2)])) {
|
|
|
44 |
$runmode[substr($arg, 2)] = true;
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
// Help mode. Shows allowed argumentents and quit directly
|
|
|
49 |
if ($runmode['help'] == true) {
|
|
|
50 |
echo 'Usage: '.$argv[0].' [runmode]' . "\n";
|
|
|
51 |
echo 'Available runmodes:' . "\n";
|
|
|
52 |
foreach ($runmode as $runmod=>$val) {
|
|
|
53 |
echo ' --'.$runmod . "\n";
|
|
|
54 |
}
|
|
|
55 |
die();
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
// Make it possible to test in source directory
|
|
|
59 |
// This is for PEAR developers only
|
|
|
60 |
ini_set('include_path', ini_get('include_path').':..');
|
|
|
61 |
|
|
|
62 |
// Include Class
|
|
|
63 |
error_reporting(E_ALL);
|
|
|
64 |
require_once 'System/Daemon.php';
|
|
|
65 |
|
|
|
66 |
// Setup
|
|
|
67 |
$options = array(
|
|
|
68 |
'appName' => 'logparser',
|
|
|
69 |
'appDir' => dirname(__FILE__),
|
|
|
70 |
'appDescription' => 'Parses vsftpd logfiles and stores them in MySQL',
|
|
|
71 |
'authorName' => 'Kevin van Zonneveld',
|
|
|
72 |
'authorEmail' => 'kevin@vanzonneveld.net',
|
|
|
73 |
'sysMaxExecutionTime' => '0',
|
|
|
74 |
'sysMaxInputTime' => '0',
|
|
|
75 |
'sysMemoryLimit' => '1024M',
|
|
|
76 |
'appRunAsGID' => 1000,
|
|
|
77 |
'appRunAsUID' => 1000,
|
|
|
78 |
);
|
|
|
79 |
|
|
|
80 |
System_Daemon::setOptions($options);
|
|
|
81 |
|
|
|
82 |
// This program can also be run in the forground with runmode --no-daemon
|
|
|
83 |
if (!$runmode['no-daemon']) {
|
|
|
84 |
// Spawn Daemon
|
|
|
85 |
System_Daemon::start();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// With the runmode --write-initd, this program can automatically write a
|
|
|
89 |
// system startup file called: 'init.d'
|
|
|
90 |
// This will make sure your daemon will be started on reboot
|
|
|
91 |
if (!$runmode['write-initd']) {
|
|
|
92 |
System_Daemon::info('not writing an init.d script this time');
|
|
|
93 |
} else {
|
|
|
94 |
if (($initd_location = System_Daemon::writeAutoRun()) === false) {
|
|
|
95 |
System_Daemon::notice('unable to write init.d script');
|
|
|
96 |
} else {
|
|
|
97 |
System_Daemon::info(
|
|
|
98 |
'sucessfully written startup script: %s',
|
|
|
99 |
$initd_location
|
|
|
100 |
);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Run your code
|
|
|
105 |
// Here comes your own actual code
|
|
|
106 |
|
|
|
107 |
// This variable gives your own code the ability to breakdown the daemon:
|
|
|
108 |
$runningOkay = true;
|
|
|
109 |
|
|
|
110 |
// This variable keeps track of how many 'runs' or 'loops' your daemon has
|
|
|
111 |
// done so far. For example purposes, we're quitting on 3.
|
|
|
112 |
$cnt = 1;
|
|
|
113 |
|
|
|
114 |
// While checks on 3 things in this case:
|
|
|
115 |
// - That the Daemon Class hasn't reported it's dying
|
|
|
116 |
// - That your own code has been running Okay
|
|
|
117 |
// - That we're not executing more than 3 runs
|
|
|
118 |
while (!System_Daemon::isDying() && $runningOkay && $cnt <=3) {
|
|
|
119 |
// What mode are we in?
|
|
|
120 |
$mode = '"'.(System_Daemon::isInBackground() ? '' : 'non-' ).
|
|
|
121 |
'daemon" mode';
|
|
|
122 |
|
|
|
123 |
// Log something using the Daemon class's logging facility
|
|
|
124 |
// Depending on runmode it will either end up:
|
|
|
125 |
// - In the /var/log/logparser.log
|
|
|
126 |
// - On screen (in case we're not a daemon yet)
|
|
|
127 |
System_Daemon::info('{appName} running in %s %s/3',
|
|
|
128 |
$mode,
|
|
|
129 |
$cnt
|
|
|
130 |
);
|
|
|
131 |
|
|
|
132 |
// In the actuall logparser program, You could replace 'true'
|
|
|
133 |
// With e.g. a parseLog('vsftpd') function, and have it return
|
|
|
134 |
// either true on success, or false on failure.
|
|
|
135 |
$runningOkay = true;
|
|
|
136 |
//$runningOkay = parseLog('vsftpd');
|
|
|
137 |
|
|
|
138 |
// Should your parseLog('vsftpd') return false, then
|
|
|
139 |
// the daemon is automatically shut down.
|
|
|
140 |
// An extra log entry would be nice, we're using level 3,
|
|
|
141 |
// which is critical.
|
|
|
142 |
// Level 4 would be fatal and shuts down the daemon immediately,
|
|
|
143 |
// which in this case is handled by the while condition.
|
|
|
144 |
if (!$runningOkay) {
|
|
|
145 |
System_Daemon::err('parseLog() produced an error, '.
|
|
|
146 |
'so this will be my last run');
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
// Relax the system by sleeping for a little bit
|
|
|
150 |
// iterate also clears statcache
|
|
|
151 |
System_Daemon::iterate(2);
|
|
|
152 |
|
|
|
153 |
$cnt++;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
// Shut down the daemon nicely
|
|
|
157 |
// This is ignored if the class is actually running in the foreground
|
|
|
158 |
System_Daemon::stop();
|