| 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 signals.php
|
|
|
29 |
* OR:
|
|
|
30 |
* killall -9 php
|
|
|
31 |
*/
|
|
|
32 |
|
|
|
33 |
// Make it possible to test in source directory
|
|
|
34 |
// This is for PEAR developers only
|
|
|
35 |
ini_set('include_path', ini_get('include_path').':..');
|
|
|
36 |
|
|
|
37 |
// Include Class
|
|
|
38 |
error_reporting(E_ALL);
|
|
|
39 |
require_once 'System/Daemon.php';
|
|
|
40 |
|
|
|
41 |
// Setup
|
|
|
42 |
System_Daemon::setOptions(array(
|
|
|
43 |
'appName' => 'signals',
|
|
|
44 |
'appDir' => dirname(__FILE__),
|
|
|
45 |
'appDescription' => 'Showcases how you could catch POSIX signals',
|
|
|
46 |
'authorName' => 'Kevin van Zonneveld',
|
|
|
47 |
'authorEmail' => 'kevin@vanzonneveld.net',
|
|
|
48 |
'sysMaxExecutionTime' => '0',
|
|
|
49 |
'sysMaxInputTime' => '0',
|
|
|
50 |
'sysMemoryLimit' => '1024M',
|
|
|
51 |
'appRunAsGID' => 1000,
|
|
|
52 |
'appRunAsUID' => 1000,
|
|
|
53 |
));
|
|
|
54 |
|
|
|
55 |
// Overrule the signal handler with any function
|
|
|
56 |
System_Daemon::setSigHandler(SIGTERM, 'myHandler');
|
|
|
57 |
|
|
|
58 |
function myHandler($signal) {
|
|
|
59 |
if ($signal === SIGTERM) {
|
|
|
60 |
System_Daemon::warning('I received the termination signal. ' . $sig);
|
|
|
61 |
// Execute some final code
|
|
|
62 |
// and be sure to:
|
|
|
63 |
System_Daemon::stop();
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
// Spawn Daemon
|
|
|
69 |
System_Daemon::start();
|
|
|
70 |
|
|
|
71 |
// Here comes your own actual code
|
|
|
72 |
|
|
|
73 |
// This variable keeps track of how many 'runs' or 'loops' your daemon has
|
|
|
74 |
// done so far. For example purposes, we're quitting on 3.
|
|
|
75 |
$cnt = 1;
|
|
|
76 |
|
|
|
77 |
// While checks on 2 things in this case:
|
|
|
78 |
// - That the Daemon Class hasn't reported it's dying
|
|
|
79 |
// - That we're not executing more than 3 runs
|
|
|
80 |
while (!System_Daemon::isDying() && $cnt <=3) {
|
|
|
81 |
// Log something using the Daemon class's logging facility
|
|
|
82 |
// Depending on runmode it will either end up:
|
|
|
83 |
// - In the /var/log/logparser.log
|
|
|
84 |
// - On screen (in case we're not a daemon yet)
|
|
|
85 |
System_Daemon::info('{appName} running %s/3',
|
|
|
86 |
$cnt
|
|
|
87 |
);
|
|
|
88 |
|
|
|
89 |
// Relax the system by sleeping for a little bit
|
|
|
90 |
// iterate() also clears statcache
|
|
|
91 |
System_Daemon::iterate(2);
|
|
|
92 |
|
|
|
93 |
// Just here to showcase how sighandlers can work
|
|
|
94 |
// to catch a
|
|
|
95 |
// /etc/init.d/signals stop
|
|
|
96 |
// The SIGTERM signal tells the daemon to quit.
|
|
|
97 |
// Normally it's catched by the ::defaultSigHandler()
|
|
|
98 |
// but now we catch it with myHandler()
|
|
|
99 |
posix_kill(posix_getpid(), SIGTERM);
|
|
|
100 |
|
|
|
101 |
$cnt++;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// Shut down the daemon nicely
|
|
|
105 |
// This is ignored if the class is actually running in the foreground
|
|
|
106 |
System_Daemon::stop();
|