Back to DaemonApplication class

Method detach

protected void
detach
()
This is truly where the magic happens. This is where we fork the process and kill the parent process, which is essentially what turns the application into a daemon.
Returns
  • void
Since
  • 3.0.0
-
  • \RuntimeException

Method detach - Source code

/**
 * This is truly where the magic happens.  This is where we fork the process and kill the parent
 * process, which is essentially what turns the application into a daemon.
 *
 * @return  void
 *
 * @since   3.0.0
 * @throws  \RuntimeException
 */
protected function detach()
{
    Log::add('Detaching the ' . $this->name . ' daemon.', Log::DEBUG);
    // Attempt to fork the process.
    $pid = $this->fork();
    // If the pid is positive then we successfully forked, and can close this application.
    if ($pid) {
        // Add the log entry for debugging purposes and exit gracefully.
        Log::add('Ending ' . $this->name . ' parent process', Log::DEBUG);
        $this->close();
    } else {
        // Setup some protected values.
        $this->exiting = false;
        $this->running = true;
        // Set the parent to self.
        $this->parentId = $this->processId;
    }
}