public function execute() { ob_start(); if ($this->runmodes['help'] == true) { echo 'Usage: ' . $argv[0] . ' [runmodes]' . "\n"; echo 'Available runmodes:' . "\n"; foreach ($this->runmodes as $runmode) { echo ' --' . $runmode . "\n"; } die; } System_Daemon::setOptions($this->options); if (!$this->runmodes['no-daemon']) { System_Daemon::start(); } if (!$this->runmodes['write-initd']) { System_Daemon::info('not writing an init.d script this time'); } else { if (($initd_location = System_Daemon::writeAutoRun()) === false) { System_Daemon::notice('unable to write init.d script'); } else { System_Daemon::info('sucessfully written startup script: %s', $initd_location); } } $this->application->dataLink->disconnect(); $this->application->dataLink->connect(); while (!System_Daemon::isDying() && $this->status == self::RUN) { $this->process(); System_Daemon::iterate($this->ownOptions['sleep']); $output = ob_get_contents(); if ($output != '') { System_Daemon::info($output); } ob_clean(); } }
} else { System_Daemon::log(System_Daemon::LOG_INFO, "sucessfully written " . "startup script: " . $initd_location); } } // This variable gives your own code the ability to breakdown the daemon: $runningOkay = true; require "Mercury.class.php"; $core = new Mercury(); if (!$core->setup_account("twitter", "example", "example")) { $runningOkay = false; System_Daemon::log(System_Daemon::LOG_ERR, System_Daemon::getOption("appName") . " failed to set up Twitter account."); } // While checks on 2 things in this case: // - That the Daemon Class hasn't reported it's dying // - 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.
} 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,
#This Program PHP Resident Program #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"]);
System_Daemon::warning('I received the termination signal. ' . $sig); // Execute some final code // and be sure to: System_Daemon::stop(); } } // Spawn Daemon System_Daemon::start(); // Here comes your own actual code // 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 2 things in this case: // - That the Daemon Class hasn't reported it's dying // - That we're not executing more than 3 runs while (!System_Daemon::isDying() && $cnt <= 3) { // 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::info('{appName} running %s/3', $cnt); // Relax the system by sleeping for a little bit // iterate() also clears statcache System_Daemon::iterate(2); // Just here to showcase how sighandlers can work // to catch a // /etc/init.d/signals stop // The SIGTERM signal tells the daemon to quit. // Normally it's catched by the ::defaultSigHandler() // but now we catch it with myHandler() posix_kill(posix_getpid(), SIGTERM);
} // enable gargabe collector if (function_exists("gc_enable")) { gc_enable(); } // spawn task manager if ($run_taskmgr == 1) { $taskmgr_pid = $ms->init_task_manager(); } // spawn statistics collector if ($run_stats == 1) { $collect_pid = $ms->init_stats_collector(); } // wait for any kill signal while (1) { if (System_Daemon::isDying()) { if ($run_taskmgr == 1) { pcntl_wait($taskmgr_pid); } if ($run_stats == 1) { pcntl_wait($collect_pid); } exit(0); } // sleep a second System_Daemon::iterate(1); } unset($db); function show_help() { print "\nshaper_agent.php - MasterShaper Agent\n(c) Andreas Unterkircher <*****@*****.**>\nhttp://www.mastershaper.org\n\n./shaper_agent.php <options>\n\n -f --foreground ... do not fork into background\n -t --taskmgr-only ... start task-manager only (load and unload rules)\n -s --stats-only ... start statistics collector only\n -h --help ... this help text\n -vx --verbose=x ... verbose level (1 info, 2 warn, 3 debug)\n\n";