| 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 New BSD Licence
|
|
|
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 |
* and stopped directly. You should find a log enty in
|
|
|
21 |
* /var/log/optest.log
|
|
|
22 |
*
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
// Make it possible to test in source directory
|
|
|
26 |
// This is for PEAR developers only
|
|
|
27 |
ini_set('include_path', ini_get('include_path').':..');
|
|
|
28 |
|
|
|
29 |
// Include Class
|
|
|
30 |
error_reporting(E_ALL);
|
|
|
31 |
require_once 'System/Daemon.php';
|
|
|
32 |
|
|
|
33 |
// Bare minimum setup
|
|
|
34 |
System_Daemon::setOption('appName', 'optest');
|
|
|
35 |
System_Daemon::setOption('authorEmail', 'kevin@vanzonneveld.net');
|
|
|
36 |
System_Daemon::setOption('logLocation', '/var/log/sysdaemon.devtest.log');
|
|
|
37 |
System_Daemon::setOption('logFilePosition', true);
|
|
|
38 |
|
|
|
39 |
System_Daemon::warning('{appName} daemon encountered an empty appPidLocation');
|
|
|
40 |
System_Daemon::err('{appName} daemon encountered an empty appPidLocation');
|
|
|
41 |
|
|
|
42 |
die();
|
|
|
43 |
|
|
|
44 |
$options = array();
|
|
|
45 |
$options['appName'] = 'devtest';
|
|
|
46 |
$options['appExecutable'] = 'devtest.php';
|
|
|
47 |
$options['appDir'] = realpath(dirname(__FILE__));
|
|
|
48 |
$options['appDescription'] = 'Developer test daemon';
|
|
|
49 |
$options['authorName'] = 'kevman';
|
|
|
50 |
$options['authorEmail'] = 'kev@man.com';
|
|
|
51 |
|
|
|
52 |
if (($os = System_Daemon_OS::factory('BSD')) === false) {
|
|
|
53 |
echo 'Cannot create OS\n';
|
|
|
54 |
} else {
|
|
|
55 |
print_r($os->errors);
|
|
|
56 |
|
|
|
57 |
echo '\n';
|
|
|
58 |
|
|
|
59 |
echo $os->getAutoRunTemplatePath();
|
|
|
60 |
echo '\n';
|
|
|
61 |
|
|
|
62 |
$details = $os->getDetails();
|
|
|
63 |
echo '\n';
|
|
|
64 |
print_r($details);
|
|
|
65 |
echo '\n';
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
die ();
|
|
|
72 |
if (($res = $os->writeAutoRun($options, true)) === false) {
|
|
|
73 |
print_r($os->errors);
|
|
|
74 |
} elseif ($res === true) {
|
|
|
75 |
echo 'alread written\n';
|
|
|
76 |
} else {
|
|
|
77 |
echo 'written to '.$res.'\n';
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
/*if (!$os->setAutoRunProperties($options)) {
|
|
|
84 |
print_r($os->errors);
|
|
|
85 |
}
|
|
|
86 |
*/
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
die();
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
// Bare minimum setup
|
|
|
95 |
System_Daemon::setOption('appName', 'optest');
|
|
|
96 |
System_Daemon::setOption('authorEmail', 'kevin@vanzonneveld.net');
|
|
|
97 |
|
|
|
98 |
die();
|
|
|
99 |
|
|
|
100 |
//System_Daemon::setOption('appDir', dirname(__FILE__));
|
|
|
101 |
System_Daemon::log(System_Daemon::LOG_INFO, 'Daemon not yet started so '.
|
|
|
102 |
'this will be written on-screen');
|
|
|
103 |
|
|
|
104 |
// Spawn Deamon!
|
|
|
105 |
System_Daemon::start();
|
|
|
106 |
System_Daemon::log(System_Daemon::LOG_INFO, 'Daemon: \''.
|
|
|
107 |
System_Daemon::getOption('appName').
|
|
|
108 |
'\' spawned! This will be written to '.
|
|
|
109 |
System_Daemon::getOption('logLocation'));
|
|
|
110 |
|
|
|
111 |
// Your normal PHP code goes here. Only the code will run in the background
|
|
|
112 |
// so you can close your terminal session, and the application will
|
|
|
113 |
// still run.
|
|
|
114 |
|
|
|
115 |
System_Daemon::stop();
|