public function init() { if (!$this->appName) { throw new CException('Invalid application name for daemon.'); } if ($this->logFile == '') { $this->logFile = implode(DIRECTORY_SEPARATOR, array(Yii::getPathOfAlias('application'), 'runtime', self::LOG_FILE_NAME)); } if (!file_exists($this->logFile)) { if (!touch($this->logFile)) { throw new CException('Invalid log file ' . $this->logFile); } } Yii::registerAutoloader(array('System_Daemon', 'autoload')); System_Daemon::setOptions(array('appName' => $this->appName, 'appDir' => Yii::getPathOfAlias('application'), 'sysMaxExecutionTime' => $this->sysMaxExecutionTime, 'sysMaxInputTime' => $this->sysMaxInputTime, 'sysMemoryLimit' => $this->sysMemoryLimit, 'appRunAsGID' => $this->runAsGID, 'appRunAsUID' => $this->runAsUID, 'logLocation' => $this->logFile)); }
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(); } }
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" => "mercury", "appDir" => dirname(__FILE__), "appDescription" => "Informs the user of their social interactions.", "authorName" => "Alastair Dewar", "authorEmail" => "*****@*****.**", "sysMaxExecutionTime" => "0", "sysMaxInputTime" => "0", "sysMemoryLimit" => "256M", "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");
* * * In panic situations, you can always kill you daemon by typing * * killall -9 signals.php * OR: * killall -9 php */ // 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 System_Daemon::setOptions(array('appName' => 'signals', 'appDir' => dirname(__FILE__), 'appDescription' => 'Showcases how you could catch POSIX signals', 'authorName' => 'Kevin van Zonneveld', 'authorEmail' => '*****@*****.**', 'sysMaxExecutionTime' => '0', 'sysMaxInputTime' => '0', 'sysMemoryLimit' => '1024M', 'appRunAsGID' => 1000, 'appRunAsUID' => 1000)); // Overrule the signal handler with any function System_Daemon::setSigHandler(SIGTERM, 'myHandler'); function myHandler($signal) { if ($signal === SIGTERM) { 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
/** * start pcntl based daemon * * @return void */ private function _daemonStart() { $options = array('appName' => 'osclog', 'appDir' => dirname(__FILE__), 'appDescription' => 'logs osc messages', 'sysMaxExecutionTime' => '0', 'sysMaxInputTime' => '0', 'sysMemoryLimit' => '1024M'); System_Daemon::setOptions($options); System_Daemon::start(); }