Example #1
0
 /**
  * Sytem_Daemon::_die()
  * Kill the daemon
  * Keep this function as independent from complex logic as possible
  *
  * @param boolean $restart Whether to restart after die
  *
  * @return void
  */
 protected static function _die($restart = false)
 {
     if (self::isDying()) {
         return null;
     }
     self::$_isDying = true;
     // Following caused a bug if pid couldn't be written because of
     // privileges
     // || !file_exists(self::opt('appPidLocation'))
     if (!self::isInBackground()) {
         self::info('Process was not daemonized yet, ' . 'just halting current process');
         die;
     }
     $pid = file_get_contents(System_Daemon::getOption('appPidLocation'));
     @unlink(self::opt('appPidLocation'));
     if ($restart) {
         // So instead we should:
         die(exec(join(' ', $GLOBALS['argv']) . ' > /dev/null &'));
     } else {
         passthru('kill -9 ' . $pid);
         die;
     }
 }
Example #2
0
$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.
    // 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");
    }
$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
Example #4
0
 * @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();
    $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();
// 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;
    }
}
// Bare minimum setup
System_Daemon::setOption("appName", "simple");
System_Daemon::setOption("appDescription", "Testing");
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");
function fncProcessJobs()
{
    return true;
}
// Spawn Deamon!
System_Daemon::start();
$runningOkay = true;
while (!System_Daemon::isDying() && $runningOkay) {
    if ($runmode['logfirst']) {
        System_Daemon::getOption("appName");
    }
    $runningOkay = fncProcessJobs();
    echo " - " . time() . "\n";
    System_Daemon::iterate(2);
}
// 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();