// - That your own code has been running Okay while (!System_Daemon::isDying() && $runningOkay) { // 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/mercury.log // - On screen (in case we're not a daemon yet) System_Daemon::log(System_Daemon::LOG_INFO, System_Daemon::getOption("appName") . " checking all protocols"); $core->check_all_protocols(); // 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 // NOTE Cannot query twitter more than 100 times per hour sleep(60); } // Shut down the daemon nicely // This is ignored if the class is actually running in the foreground System_Daemon::stop();
* @copyright 2008 Kevin van Zonneveld * @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence * @link http://github.com/kvz/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();
* * @category System * @package System_Daemon * @author Kevin <*****@*****.**> * @copyright 2009 Kevin van Zonneveld * @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence * @link http://github.com/kvz/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", "mydaemon"); System_Daemon::setOption("appPidLocation", "/tmp/mydaemon.pid"); System_Daemon::setOption("logLocation", "/tmp/mydaemon.log"); // 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::stop();
* 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", "phperrtest"); System_Daemon::setOption("authorEmail", "*****@*****.**"); System_Daemon::setOption('logPhpErrors', true); System_Daemon::setOption('logFilePosition', true); System_Daemon::setOption('logLinePosition', true); //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")); $x = 1; System_Daemon::log(System_Daemon::LOG_WARNING, 'Test'); foreach ($x as $k => $v) { // should break and generate PHP warnings } // 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();
function d_error($msg) { if (System_Daemon::isInBackground()) { System_Daemon::log(System_Daemon::LOG_ERR, $msg); } else { echo "{$msg}\n"; } }
#And Need Mysql PHP System_Daemon #OPEN DATA http://blockchain.info/ require "Mysql PassWord Data"; require_once 'System/Daemon.php'; $app_name = "bitcoin"; $options = array('appName' => 'deamon_name', 'appDir' => dirname(__FILE__)); System_Daemon::setOptions($options); System_Daemon::start(); $mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['bitdb']); $mysqli->set_charset("utf-8"); if ($mysqli->connect_error) { echo "DB error"; die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } while (!System_Daemon::isDying()) { System_Daemon::log(System_Daemon::LOG_INFO, date('Y/m/d H:i:s')); $blockfile = file_get_contents($blockchain); $block = json_decode($blockfile, true); $USDb = floatval($block["USD"]["buy"]); $USDs = floatval($block["USD"]["sell"]); $ISKb = floatval($block["ISK"]["buy"]); $ISKs = floatval($block["ISK"]["sell"]); $HKDb = floatval($block["HKD"]["buy"]); $HKDs = floatval($block["HKD"]["sell"]); $TWDb = floatval($block["TWD"]["buy"]); $TWDs = floatval($block["TWD"]["sell"]); $CHFb = floatval($block["CHF"]["buy"]); $CHFs = floatval($block["CHF"]["sell"]); $EURb = floatval($block["EUR"]["buy"]); $EURs = floatval($block["EUR"]["sell"]); $DKKb = floatval($block["DKK"]["buy"]);
* /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", "*****@*****.**"); //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::writeAutoRun(); $id = 0; while (true) { $id++; System_Daemon::log(System_Daemon::LOG_INFO, 'ping ' . $id . "\n"); sleep(1); if ($id > 10000) { break; } } System_Daemon::stop();
$details = $os->getDetails(); echo '\\n'; print_r($details); echo '\\n'; } die; if (($res = $os->writeAutoRun($options, true)) === false) { print_r($os->errors); } elseif ($res === true) { echo 'alread written\\n'; } else { echo 'written to ' . $res . '\\n'; } /*if (!$os->setAutoRunProperties($options)) { print_r($os->errors); } */ die; // Bare minimum setup System_Daemon::setOption('appName', 'optest'); System_Daemon::setOption('authorEmail', '*****@*****.**'); die; //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();
* @license http://www.opensource.org/licenses/bsd-license.php New BSD Licence * @link http://github.com/kvz/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();
/** * Add a new follower to the table **/ protected function addFollower($data) { /** * Set up database connection */ ORM::configure('mysql:host=localhost;dbname=' . DATABASE_TABLE); ORM::configure('username', DATABASE_USERNAME); ORM::configure('password', DATABASE_PASSWORD); if ($data['screen_name'] == TWITTER_USERNAME) { return; } // TODO change to production twitter handle $follow_code = md5($data['screen_name']); $follower = ORM::for_table('followers')->where('twitter_id', $data['id'])->find_one(); if ($follower == false) { $follower = ORM::for_table('followers')->create(); $follower->twitter_id = $data['id']; $follower->username = $data['screen_name']; $follower->follow_code = $follow_code; } $follower->created = date('Y-m-d H:i:s', time()); $follower->save(); /** * Send Direct Message **/ $google = new GoogleURL(GOOGLE_API_KEY); $url = $google->shorten(WEB_APP_BASE_URL . "?follow_code={$follow_code}"); $message = "Thanks for following, please visit " . $url . " within the hour to verify your age or you'll be blocked."; $connection = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET); $message_resp = $connection->post('direct_messages/new', array('user_id' => $data['id'], 'text' => $message)); System_Daemon::log(System_Daemon::LOG_INFO, "New request to follow, direct message http code resp was " . $connection->http_code); // get rid of db connection unset($follower); // get rid of db connection }