#!/usr/bin/php -q * @copyright 2008 Kevin van Zonneveld * @license http://www.opensource.org/licenses/bsd-license.php * @link http://github.com/kvz/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 daemon-sample.php * OR: * killall -9 php * */ // Include Class error_reporting(E_ALL); require_once 'System/Daemon.php'; // Setup $options = array( 'appName' => 'daemon-sample', 'authorEmail' => 'noname@local.host', 'appDir' => dirname(__FILE__), ); System_Daemon::setOptions($options); System_Daemon::log(System_Daemon::LOG_INFO, "Daemon not yet started so "."this will be written on-screen"); // Start daemon System_Daemon::start(); // logging some of info 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. // // Below is a sample code, run this php code then create a file /tmp/darkhero.txt. // when you modified this file the message 'Detected the file has been changed!!!' would be logged into the file /var/log/daemon-sample.log if(!file_exists('/tmp/darkhero_md5.txt')) touch('/tmp/darkhero_md5.txt'); while(1){ if(file_exists('/tmp/darkhero.txt') and md5_file('/tmp/darkhero.txt') != file_get_contents('/tmp/darkhero_md5.txt')){ System_Daemon::log(System_Daemon::LOG_INFO, "Detected the file has been changed!!!"); $md5_string = md5_file('/tmp/darkhero.txt'); file_put_contents('/tmp/darkhero_md5.txt',$md5_string); } usleep(100); } // Shut down the daemon nicely // This is ignored if the class is actually running in the foreground System_Daemon::stop(); ?>