cvs: peardoc /en/package/system/system-daemon console-action.xml daemons-vs-cronjobs.xml examples.xml how-it-works-internally.xml introduction.xml possible-use-cases.xml system-daemon.xml troubleshooting.xml what-is-a-daemon.xml why-php.xml /en/package/system/system-daemon/examples logparser.php nopear.php pearlog.php simple.php simple.xml
[email protected] ("Kevin van Zonneveld")
| Newsgroups | php.pear.doc |
|---|---|
| Message-ID | <cvskvz1235148907@cvsserver> |
kvz Fri Feb 20 16:55:07 2009 UTC
Added files:
/peardoc/en/package/system/system-daemon console-action.xml
daemons-vs-cronjobs.xml
examples.xml
how-it-works-internally.xml
introduction.xml
possible-use-cases.xml
system-daemon.xml
troubleshooting.xml
what-is-a-daemon.xml
why-php.xml
/peardoc/en/package/system/system-daemon/examples logparser.php
nopear.php
pearlog.php
simple.php
simple.xml
Log:
Initial import System_Daemon docs
kvz-20090220165507.txt
(text/plain, 28.2 KB)
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/console-action.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/console-action.xml
+++ peardoc/en/package/system/system-daemon/console-action.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.console-action"
>
<info>
<title>Console action</title>
<titleabbrev>Keep your daemon on a leesh</titleabbrev>
</info>
<para>
Now that we've created an example daemon, it's time to fire
it up! I'm going to assume the name of your daemon is logparser.
This can be changed with the statement:
<programlisting role="php"><![CDATA[
<?php
System_Daemon::setOption("appName", "logparser")
?>
]]></programlisting>
But the name of the daemon is very important because it is also
used in filenames (like the logfile).
</para>
<para>
Execute
Just make your daemon script executable, and then execute it:
<programlisting role="bash"><![CDATA[
chmod a+x ./logparser.php
./logparser.php
]]></programlisting>
</para>
<para>
Check
Your daemon has no way of communicating through your console, so check for messages in:
<programlisting role="bash"><![CDATA[
tail /var/log/logparser.log
]]></programlisting>
And see if it's still running:
<programlisting role="bash"><![CDATA[
ps uf -C logparser.php
]]></programlisting>
</para>
<para>
Kill
Without the start/stop files (see below for howto), you need to:
<programlisting role="bash"><![CDATA[
killall -9 logparser.php
]]></programlisting>
Autch.. Let's work on those start / stop files, right?
</para>
<para>
Start / Stop files (Debian & Ubuntu only)
Real daemons have an init.d file. Remember you can restart Apache with the following statement?
<programlisting role="bash"><![CDATA[
/etc/init.d/apache2 restart
]]></programlisting>
That's a lot better than killing. So you should be able to control your own daemon like this as well:
<programlisting role="bash"><![CDATA[
/etc/init.d/logparser stop
/etc/init.d/logparser start
]]></programlisting>
Well with System_Daemon you can write autostartup files using the writeAutoRun() method, look:
<programlisting role="php"><![CDATA[
<?php
$path = System_Daemon::writeAutoRun();
?>
]]></programlisting>
On success, this will return the path to the autostartup file: /etc/init.d/logparser, and you're good to go!
</para>
<para>
Run on boot
Surely you want your daemon to run at system boot.. So on Debian & Ubuntu you could type:
<programlisting role="bash"><![CDATA[
update-rc.d logparser defaults
]]></programlisting>
Done your daemon now starts every time your server boots. Cancel it with:
<programlisting role="bash"><![CDATA[
update-rc.d -f logparser remove
]]></programlisting>
</para>
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/daemons-vs-cronjobs.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/daemons-vs-cronjobs.xml
+++ peardoc/en/package/system/system-daemon/daemons-vs-cronjobs.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.daemons-vs-cronjobs"
>
<info>
<title>What is a Daemon?</title>
<titleabbrev>Introduction to daemons</titleabbrev>
</info>
<para>
Some people use cronjobs for the same Possible use cases. Crontab is
fine but it only allows you to run a PHP file every minute or so.
<itemizedlist>
<listitem>
<para>
What if the previous run hasn't finished yet? Overlap can seriously damage your data & cause siginificant load on your machines.
</para>
</listitem>
<listitem>
<para>
What if you can't afford to wait a minute for the cronjob to run? Maybe you need to trigger a process the moment a record is inserted?
</para>
</listitem>
<listitem>
<para>
What if you want to keep track of multiple 'runs' and store data in memory.
</para>
</listitem>
<listitem>
<para>
What if you need to keep your application listening (on a socket for example)
</para>
</listitem>
</itemizedlist>
Cronjobs are a bit rude for this, they may spin out of control and
don't provide a framework for logging, etc. Creating a daemon would
offer more elegance & possibilities. Let's just say: there are very
good reasons why a Linux OS isn't composed entirely of Cronjobs :)
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/examples.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/examples.xml
+++ peardoc/en/package/system/system-daemon/examples.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.examples"
>
<info>
<title>System_Daemon examples</title>
<titleabbrev>Examples</titleabbrev>
</info>
<para>
To get the feeling how Console_Table is used, try out the following examples.
</para>
&package.system.system-daemon.examples.simple;
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/how-it-works-internally.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/how-it-works-internally.xml
+++ peardoc/en/package/system/system-daemon/how-it-works-internally.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.how-it-works-internally"
>
<info>
<title>How it works internally</title>
<titleabbrev>Introduction to daemons</titleabbrev>
</info>
<para>
Nerd alert!) When a daemon program is started, it fires up a second
child process, detaches it, and then the parent process dies. This is
called forking. Because the parent process dies, it will give the
console back and it will look like nothing has happened. But wait:
the child process is still running. Even if you close your terminal,
the child continues to run in memory, until it either stops, crashes
or is killed.
In PHP: forking can be achieved by using the Process Control Extensions.
Getting a good grip on it, may take some studying though.
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/introduction.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/introduction.xml
+++ peardoc/en/package/system/system-daemon/introduction.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.introduction"
>
<info>
<title>Introduction to System_Daemon</title>
<titleabbrev>Introduction</titleabbrev>
</info>
<para>
Everyone knows PHP can be used to create websites.
But it can also be used to create desktop applications and commandline tools.
And now with a class called <phd:pearapi phd:package="System_Daemon"/>, you can
even create daemons using nothing but PHP. And did I mention it was easy?
</para>
<para>
This how-to was ported from:
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/
you can leave a comment there as well.
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/possible-use-cases.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/possible-use-cases.xml
+++ peardoc/en/package/system/system-daemon/possible-use-cases.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.possible-use-cases"
>
<info>
<title>Possible use cases</title>
<titleabbrev>What can you do with <classname>System_Daemon</classname>?</titleabbrev>
</info>
<para>
<itemizedlist>
<listitem>
<para>
Website optimization
If you're running a (large) website, jobs that do heavy lifting should be taken away from the user interface and scheduled to run on the machine separately.
</para>
</listitem>
<listitem>
<para>
Log parser
Continually scan logfiles and import critical messages in your database.
</para>
</listitem>
<listitem>
<para>
SMS daemon
Read a database queue, and let your little daemon interface with your SMS provider. If it fails, it can easily try again later!
</para>
</listitem>
<listitem>
<para>
Video converter (think Youtube)
Scan a queue/directory for incoming video uploads. Make some system calls to ffmpeg to finally store them as Flash video files. Surely you don't want to convert video files right after the upload, blocking the user interface that long? No: the daemon will send the uploader a mail when the conversion is done, and proceed with the next scheduled upload
</para>
</listitem>
</itemizedlist>
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/system-daemon.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/system-daemon.xml
+++ peardoc/en/package/system/system-daemon/system-daemon.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.system-daemon"
>
<info>
<title><classname>System_Daemon</classname></title>
<titleabbrev>Let's wrap it up</titleabbrev>
</info>
<para>
Because the Process Control Extensions' documentation is a bit rough,
I decided to figure it out once, and then wrap my knowledge and the
required code inside a PEAR class called: System_Daemon. And so now
you can just:
<example><info><title><classname>System_Daemon</classname> Usage</title></info>
<programlisting role="php"><![CDATA[
<?php
require_once "System/Daemon.php"; // Include the Class
System_Daemon::setOption("appName", "mydaemon"); // Minimum configuration
System_Daemon::start(); // Spawn Deamon!
?>
]]></programlisting>
</example>
And that's all there is to it. The code after that, will run in your
server's background. So next, if you create a while(true) loop
around that code, the code will run indefinitely. Remember to build
in a sleep(5) to ease up on system resources.
</para>
<para>
Features & Characteristics
Here's a grab of System_Daemon's features:
<itemizedlist>
<listitem>
<para>
Daemonize any PHP-CLI script
</para>
</listitem>
<listitem>
<para>
Simple syntax
</para>
</listitem>
<listitem>
<para>
Driver based Operating System detection
</para>
</listitem>
<listitem>
<para>
Unix only
</para>
</listitem>
<listitem>
<para>
Additional features for Debian / Ubuntu based systems like:
</para>
</listitem>
<listitem>
<para>
Automatically generate startup files (init.d)
</para>
</listitem>
<listitem>
<para>
Support for PEAR's Log package
</para>
</listitem>
<listitem>
<para>
Can run with PEAR (more elegance & functionality) or without PEAR (for standalone packages)
</para>
</listitem>
<listitem>
<para>
Default signal handlers, but optionally reroute signals to your own handlers.
</para>
</listitem>
<listitem>
<para>
Set options like max RAM usage
</para>
</listitem>
</itemizedlist>
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/troubleshooting.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/troubleshooting.xml
+++ peardoc/en/package/system/system-daemon/troubleshooting.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.troubleshooting"
>
<info>
<title>Troubleshooting</title>
<titleabbrev>Problems you may encounter</titleabbrev>
</info>
<para>
Here are some issues you may encounter down the road.
<itemizedlist>
<listitem>
<para>
Connect to MySQL after you start() the daemon.
Otherwise only the parent process will have a MySQL connection, and
since that dies.. It's lost and you will get a 'MySQL has gone away'
error.
</para>
</listitem>
<listitem>
<para>
Error handling
Good error handling is imperative. Daemons are often mission critical
applications and you don't want an uncatched error to bring it to it's
knees.
<itemizedlist>
<listitem>
<para>
Reconnect to MySQL
A connection may be interrupted. Think about network downtime or
lock-ups when your database server makes backups. Whatever the
cause: You don't want your daemon to die for this, let it try
again later.
</para>
</listitem>
<listitem>
<para>
Write your own php error handler
It should forward all the PHP errors to the log() method, so they end
up in your logfile. Otherwise it's very hard to spot errors, since the
daemon has no way of writing to your console anymore! See
set_error_handler.
</para>
</listitem>
</itemizedlist>
</para>
</listitem>
<listitem>
<para>
Monit
Monit is a standalone program that can kickstart any daemon,
based on your parameters. Should your daemon fail, monit will
mail you and try to restart it.
</para>
</listitem>
</itemizedlist>
</para>
<para>
I know I'm saying MySQL a lot, but you can obviously replace that
with Oracle, MSSQL, PostgreSQL, etc.
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/what-is-a-daemon.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/what-is-a-daemon.xml
+++ peardoc/en/package/system/system-daemon/what-is-a-daemon.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.what-is-a-daemon"
>
<info>
<title>What is a Daemon?</title>
<titleabbrev>Introduction to daemons</titleabbrev>
</info>
<para>
A daemon is a Linux program that run in the background, just
like a 'Service' on Windows. It can perform all sorts of tasks
that do not require direct user input. Apache is a daemon, so
is MySQL. All you ever hear from them is found in somewhere in
<literal>/var/log</literal>, yet they silently power over 40%
of the Internet.
You reading this page, would not have been possible without them.
So clearly: a daemon is a powerful thing, and can be bend to do a
lot of different tasks.
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/why-php.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/why-php.xml
+++ peardoc/en/package/system/system-daemon/why-php.xml
<?xml version="1.0" encoding="utf-8"?>
<chapter
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
version="lillet"
xml:id="package.system.system-daemon.why-php"
>
<info>
<title>Why PHP?</title>
<titleabbrev>Aren't you supposed to use C++?</titleabbrev>
</info>
<para>
Most daemons are written in C. It's fast & robust. But if you are
in a LAMP oriented company like me, and you need to create a lot
of software in PHP anyway, it makes sense:
<itemizedlist>
<listitem>
<para>
Efficiency. Reuse & connect existing code
Think of database connections, classes that create customers from
your CRM, etc.
</para>
</listitem>
<listitem>
<para>
Speed. Deliver new applications very fast
PHP has a lot of build in functions that speed up development
greatly.
</para>
</listitem>
<listitem>
<para>
Continuity. Everyone knows PHP (right?)
If you work in a small company: chances are there are more PHP
programmers than there are C programmers. What if your C guy
abandons ship?
</para>
</listitem>
</itemizedlist>
</para>
</chapter>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/examples/logparser.php?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/examples/logparser.php
+++ peardoc/en/package/system/system-daemon/examples/logparser.php
#!/usr/bin/php -q
<?php
/**
* System_Daemon turns PHP-CLI scripts into daemons.
*
* PHP version 5
*
* @category System
* @package System_Daemon
* @author Kevin <[email protected]>
* @copyright 2008 Kevin van Zonneveld
* @license http://www.opensource.org/licenses/bsd-license.php
* @version SVN: Release: $Id: logparser.php,v 1.1 2009/02/20 16:55:07 kvz Exp $
* @link http://trac.plutonia.nl/projects/system_daemon
*/
/**
* System_Daemon Example Code
*
* If you run this code successfully, a daemon will be spawned
* but unless have already generated the init.d script, you have
* no real way of killing it yet.
*
* In this case wait 3 runs, which is the maximum for this example.
*
*
* In panic situations, you can always kill you daemon by typing
*
* killall -9 logparser.php
* OR:
* killall -9 php
*
*/
// Allowed arguments & their defaults
$runmode = array(
"no-daemon" => false,
"help" => false,
"write-initd" => false
);
// Scan command line attributes for allowed arguments
foreach ($argv as $k=>$arg) {
if (substr($arg, 0, 2) == "--" && isset($runmode[substr($arg, 2)])) {
$runmode[substr($arg, 2)] = true;
}
}
// Help mode. Shows allowed argumentents and quit directly
if ($runmode["help"] == true) {
echo "Usage: ".$argv[0]." [runmode]\n";
echo "Available runmodes:\n";
foreach ($runmode as $runmod=>$val) {
echo " --".$runmod."\n";
}
die();
}
// Make it possible to test in source directory
// This is for PEAR developers only
ini_set('include_path', ini_get('include_path').':..');
// Include Class
error_reporting(E_ALL);
require_once "System/Daemon.php";
// Setup
$options = array(
"appName" => "logparser",
"appDir" => dirname(__FILE__),
"appDescription" => "Parses vsftpd logfiles and stores them in MySQL",
"authorName" => "Kevin van Zonneveld",
"authorEmail" => "[email protected]",
"sysMaxExecutionTime" => "0",
"sysMaxInputTime" => "0",
"sysMemoryLimit" => "1024M",
"appRunAsGID" => 1000,
"appRunAsUID" => 1000
);
System_Daemon::setOptions($options);
// Overrule the signal handler with any function
System_Daemon::setSigHandler(SIGCONT, array("System_Daemon",
"defaultSigHandler"));
// This program can also be run in the forground with runmode --no-daemon
if (!$runmode["no-daemon"]) {
// Spawn Daemon
System_Daemon::start();
}
// With the runmode --write-initd, this program can automatically write a
// system startup file called: 'init.d'
// This will make sure your daemon will be started on reboot
if (!$runmode["write-initd"]) {
System_Daemon::log(System_Daemon::LOG_INFO, "not writing ".
"an init.d script this time");
} else {
if (($initd_location = System_Daemon::writeAutoRun()) === false) {
System_Daemon::log(System_Daemon::LOG_NOTICE, "unable to write ".
"init.d script");
} else {
System_Daemon::log(System_Daemon::LOG_INFO, "sucessfully written ".
"startup script: ".$initd_location);
}
}
// Run your code
// Here comes your own actual code
// This variable gives your own code the ability to breakdown the daemon:
$runningOkay = true;
// This variable keeps track of how many 'runs' or 'loops' your daemon has
// done so far. For example purposes, we're quitting on 3.
$cnt = 1;
// While checks on 3 things in this case:
// - That the Daemon Class hasn't reported it's dying
// - That your own code has been running Okay
// - That we're not executing more than 3 runs
while (!System_Daemon::isDying() && $runningOkay && $cnt <=3) {
// What mode are we in?
$mode = "'".(System_Daemon::isInBackground() ? "" : "non-" ).
"daemon' mode";
// Log something using the Daemon class's logging facility
// Depending on runmode it will either end up:
// - In the /var/log/logparser.log
// - On screen (in case we're not a daemon yet)
System_Daemon::log(System_Daemon::LOG_INFO,
System_Daemon::getOption("appName").
" running in ".$mode." ".$cnt."/3");
// In the actuall logparser program, You could replace 'true'
// With e.g. a parseLog('vsftpd') function, and have it return
// either true on success, or false on failure.
$runningOkay = true;
//$runningOkay = parseLog('vsftpd');
// Should your parseLog('vsftpd') return false, then
// the daemon is automatically shut down.
// An extra log entry would be nice, we're using level 3,
// which is critical.
// Level 4 would be fatal and shuts down the daemon immediately,
// which in this case is handled by the while condition.
if (!$runningOkay) {
System_Daemon::log(System_Daemon::LOG_ERR, "parseLog() ".
"produced an error, ".
"so this will be my last run");
}
// Relax the system by sleeping for a little bit
sleep(2);
$cnt++;
}
// Shut down the daemon nicely
// This is ignored if the class is actually running in the foreground
System_Daemon::stop();
?>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/examples/nopear.php?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/examples/nopear.php
+++ peardoc/en/package/system/system-daemon/examples/nopear.php
#!/usr/bin/php -q
<?php
/**
* System_Daemon turns PHP-CLI scripts into daemons.
*
* PHP version 5
*
* @category System
* @package System_Daemon
* @author Kevin <[email protected]>
* @copyright 2008 Kevin van Zonneveld
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* @version SVN: Release: $Id: nopear.php,v 1.1 2009/02/20 16:55:07 kvz Exp $
* @link http://trac.plutonia.nl/projects/system_daemon
*/
/**
* System_Daemon Example Code
*
* If you run this code successfully, a daemon will be spawned
* and stopped directly. You should find a log enty in
* /var/log/simple.log
*
*/
// Make it possible to test in source directory
// This is for PEAR developers only
ini_set('include_path', ini_get('include_path').':..');
// Include Class
error_reporting(E_ALL);
require_once "System/Daemon.php";
// No PEAR, run standalone
System_Daemon::setOption("usePEAR", false);
// Bare minimum setup
System_Daemon::setOption("appName", "nopear");
System_Daemon::setOption("appDir", dirname(__FILE__));
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon not yet started so this ".
"will be written on-screen");
// Spawn Deamon!
System_Daemon::start();
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon: '".
System_Daemon::getOption("appName").
"' spawned! This will be written to ".
System_Daemon::getOption("logLocation"));
// Your normal PHP code goes here. Only the code will run in the background
// so you can close your terminal session, and the application will
// still run.
System_Daemon::stop();
?>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/examples/pearlog.php?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/examples/pearlog.php
+++ peardoc/en/package/system/system-daemon/examples/pearlog.php
#!/usr/bin/php -q
<?php
/**
* System_Daemon turns PHP-CLI scripts into daemons.
*
* PHP version 5
*
* @category System
* @package System_Daemon
* @author Kevin <[email protected]>
* @copyright 2008 Kevin van Zonneveld
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* @version SVN: Release: $Id: pearlog.php,v 1.1 2009/02/20 16:55:07 kvz Exp $
* @link http://trac.plutonia.nl/projects/system_daemon
*/
/**
* System_Daemon Example Code
*
* If you run this code successfully, a daemon will be spawned
* and stopped directly. You should find a log enty in
* /var/log/pearlog.log
*
*/
// Make it possible to test in source directory
// This is for PEAR developers only
ini_set('include_path', ini_get('include_path').':..');
// Include Class
error_reporting(E_ALL);
require_once "System/Daemon.php";
// Initialize PEAR_Log instance
$my_log_instance = &Log::factory('file', '/tmp/pearlog.log', 'pearlog');
// Bare minimum setup
System_Daemon::setOption("appName", "pearlog");
System_Daemon::setOption("appDir", dirname(__FILE__));
System_Daemon::setOption("usePEARLogInstance", $my_log_instance);
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon not yet started. ".
"Every logline will end up in whatever usePEARLogInstance->log() says");
// Spawn Deamon!
System_Daemon::start();
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon started. ".
"Every logline will end up in whatever usePEARLogInstance->log() says");
// Your normal PHP code goes here. Only the code will run in the background
// so you can close your terminal session, and the application will
// still run.
System_Daemon::stop();
?>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/examples/simple.php?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/examples/simple.php
+++ peardoc/en/package/system/system-daemon/examples/simple.php
#!/usr/bin/php -q
<?php
/**
* System_Daemon turns PHP-CLI scripts into daemons.
*
* PHP version 5
*
* @category System
* @package System_Daemon
* @author Kevin <[email protected]>
* @copyright 2008 Kevin van Zonneveld
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence
* @version SVN: Release: $Id: simple.php,v 1.1 2009/02/20 16:55:07 kvz Exp $
* @link http://trac.plutonia.nl/projects/system_daemon
*/
/**
* System_Daemon Example Code
*
* If you run this code successfully, a daemon will be spawned
* and stopped directly. You should find a log enty in
* /var/log/simple.log
*
*/
// Make it possible to test in source directory
// This is for PEAR developers only
ini_set('include_path', ini_get('include_path').':..');
// Include Class
error_reporting(E_ALL);
require_once "System/Daemon.php";
// Bare minimum setup
System_Daemon::setOption("appName", "simple");
System_Daemon::setOption("authorEmail", "[email protected]");
//System_Daemon::setOption("appDir", dirname(__FILE__));
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon not yet started so ".
"this will be written on-screen");
// Spawn Deamon!
System_Daemon::start();
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon: '".
System_Daemon::getOption("appName").
"' spawned! This will be written to ".
System_Daemon::getOption("logLocation"));
// Your normal PHP code goes here. Only the code will run in the background
// so you can close your terminal session, and the application will
// still run.
System_Daemon::stop();
?>
http://cvs.php.net/viewvc.cgi/peardoc/en/package/system/system-daemon/examples/simple.xml?view=markup&rev=1.1
Index: peardoc/en/package/system/system-daemon/examples/simple.xml
+++ peardoc/en/package/system/system-daemon/examples/simple.xml
<?xml version="1.0" encoding="utf-8"?>
<section
xmlns="http://docbook.org/ns/docbook"
xmlns:phd="http://www.php.net/ns/phd"
xml:id="package.system.system-daemon.examples.simple"
>
<info>
<title>System_Daemon examples</title>
<titleabbrev>Simple</titleabbrev>
</info>
<para>
If you run this code successfully, a daemon will be spawned
and stopped directly. You should find a log enty in
<literal>/var/log/simple.log</literal>
</para>
<programlisting role="php">
<xi:include parse="text"
xmlns:xi="http://www.w3.org/2001/XInclude"
href="&package.system.system-daemon.examples.simple.php;"
>
<xi:fallback>FIXME:MISSING XINCLUDE CONTENT</xi:fallback>
</xi:include>
</programlisting>
</section>