function stop() { if (file_exists($this->command_file())) { unlink($this->command_file()); } System_Daemon::stop(); }
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(); } }
public function runTaskAsDaemon($task_name, $options = array()) { $this->_ensurePosixAndPcntlAreAvailable(); require_once 'System/Daemon.php'; $app_name = AkInflector::underscore($task_name); $pid_file = AK_BASE_DIR . DS . 'run' . DS . $app_name . DS . $app_name . '.pid'; $log_file = AK_LOG_DIR . DS . 'daemons' . DS . $app_name . '.log'; if (!file_exists($pid_file)) { if (empty($options['attributes']['kill'])) { AkFileSystem::file_put_contents($pid_file, ''); AkFileSystem::file_delete($pid_file); } else { $this->error("Could not kill process for {$task_name}", true); } } else { $pid = (int) file_get_contents($pid_file); if ($pid > 0) { if (!empty($options['attributes']['kill'])) { $this->message("Killing process {$pid}"); `kill {$pid}`; AkFileSystem::file_delete($pid_file); die; } elseif (!empty($options['attributes']['restart'])) { $this->message("Restarting {$task_name}."); $this->message(`kill {$pid}`); } else { $this->error("Daemon for {$task_name} still running ({$pid_file}).\nTask aborted.", true); } } } if (!empty($options['attributes']['kill']) && empty($pid)) { $this->error("No daemon running for task {$task_name}", true); } unset($options['attributes']['restart']); if (!file_exists($log_file)) { AkFileSystem::file_put_contents($log_file, ''); } System_Daemon::setOption('appName', $app_name); System_Daemon::setOption('appDir', AK_BASE_DIR); System_Daemon::setOption('logLocation', $log_file); System_Daemon::setOption('appRunAsUID', posix_geteuid()); System_Daemon::setOption('appRunAsGID', posix_getgid()); System_Daemon::setOption('appPidLocation', $pid_file); $this->message("Staring daemon. ({$log_file})"); System_Daemon::start(); $dsn = Ak::getStaticVar('dsn'); defined('AK_SKIP_DB_CONNECTION') && AK_SKIP_DB_CONNECTION ? null : Ak::db($dsn); $this->runTask($task_name, $options); System_Daemon::stop(); AkFileSystem::file_delete($pid_file); die; }
$appname = "ykval-queue"; System_Daemon::setOption("appName", $appname); System_Daemon::setOption("appDescription", "Yubico val-server sync daemon"); System_Daemon::setOption("authorName", "*****@*****.**"); System_Daemon::setOption("authorEmail", "*****@*****.**"); if ($argc == 2 && strcmp($argv[1], "install") == 0) { $autostart_path = System_Daemon::writeAutoRun(); if ($autostart_path != 1) { echo "Successfully created start script at " . $autostart_path . "\n"; echo "To start daemon use: /etc/init.d/" . $appname . " start\n"; } else { echo "Start script already created\n"; echo "To start daemon use: /etc/init.d/" . $appname . " start\n"; } exit; } require_once 'ykval-synclib.php'; require_once 'ykval-config.php'; require_once 'ykval-log.php'; System_Daemon::start(); // Spawn Deamon! /* Application start */ $sl = new SyncLib('ykval-queue:synclib'); # Loop forever and resync $res == 0; while ($res == 0) { $sl->reSync($baseParams['__YKVAL_SYNC_OLD_LIMIT__'], $baseParams['__YKVAL_SYNC_RESYNC_TIMEOUT__']); $res = sleep($baseParams['__YKVAL_SYNC_INTERVAL__']); } System_Daemon::stop();