Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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" => "custtemplate",
69
    "appDir" => dirname(__FILE__),
70
    "appDescription" => "Writes an init.d file based on custom template",
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
    "runTemplateLocation" => realpath(dirname(__FILE__).'/../data/template_Debian'),
79
);
80
 
81
System_Daemon::setOptions($options);
82
 
83
// Overrule the signal handler with any function
84
System_Daemon::setSigHandler(SIGCONT, array("System_Daemon",
85
    "defaultSigHandler"));
86
 
87
 
88
// This program can also be run in the forground with runmode --no-daemon
89
if (!$runmode["no-daemon"]) {
90
    // Spawn Daemon
91
    System_Daemon::start();
92
}
93
 
94
// With the runmode --write-initd, this program can automatically write a
95
// system startup file called: 'init.d'
96
// This will make sure your daemon will be started on reboot
97
if (!$runmode["write-initd"]) {
98
    System_Daemon::log(System_Daemon::LOG_INFO, "not writing ".
99
        "an init.d script this time");
100
} else {
101
    if (($initd_location = System_Daemon::writeAutoRun()) === false) {
102
        System_Daemon::log(System_Daemon::LOG_NOTICE, "unable to write ".
103
            "init.d script");
104
    } elseif ($initd_location === true) {
105
        System_Daemon::log(System_Daemon::LOG_INFO, "startup script was already there");
106
    } else {
107
        System_Daemon::log(System_Daemon::LOG_INFO, "sucessfully written ".
108
            "startup script: ".$initd_location);
109
    }
110
}
111
 
112
// Run your code
113
// Here comes your own actual code
114
 
115
// This variable gives your own code the ability to breakdown the daemon:
116
$runningOkay = true;
117
 
118
// This variable keeps track of how many 'runs' or 'loops' your daemon has
119
// done so far. For example purposes, we're quitting on 3.
120
$cnt = 1;
121
 
122
// While checks on 3 things in this case:
123
// - That the Daemon Class hasn't reported it's dying
124
// - That your own code has been running Okay
125
// - That we're not executing more than 3 runs
126
while (!System_Daemon::isDying() && $runningOkay && $cnt <=3) {
127
    // What mode are we in?
128
    $mode = "'".(System_Daemon::isInBackground() ? "" : "non-" ).
129
        "daemon' mode";
130
 
131
    // Log something using the Daemon class's logging facility
132
    // Depending on runmode it will either end up:
133
    //  - In the /var/log/logparser.log
134
    //  - On screen (in case we're not a daemon yet)
135
    System_Daemon::log(System_Daemon::LOG_INFO,
136
        System_Daemon::getOption("appName").
137
        " running in ".$mode." ".$cnt."/3");
138
 
139
    // In the actuall logparser program, You could replace 'true'
140
    // With e.g. a  parseLog('vsftpd') function, and have it return
141
    // either true on success, or false on failure.
142
    $runningOkay = true;
143
    //$runningOkay = parseLog('vsftpd');
144
 
145
    // Should your parseLog('vsftpd') return false, then
146
    // the daemon is automatically shut down.
147
    // An extra log entry would be nice, we're using level 3,
148
    // which is critical.
149
    // Level 4 would be fatal and shuts down the daemon immediately,
150
    // which in this case is handled by the while condition.
151
    if (!$runningOkay) {
152
        System_Daemon::log(System_Daemon::LOG_ERR, "parseLog() ".
153
            "produced an error, ".
154
            "so this will be my last run");
155
    }
156
 
157
    // Relax the system by sleeping for a little bit
158
    sleep(2);
159
    $cnt++;
160
}
161
 
162
// Shut down the daemon nicely
163
// This is ignored if the class is actually running in the foreground
164
System_Daemon::stop();