PHP code example of wapmorgan / system-daemon

1. Go to this page and download the library: Download wapmorgan/system-daemon library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wapmorgan / system-daemon example snippets


// adjust here path to composer autoloader!

use wapmorgan\SystemDaemon\DaemonManager;

$daemon = new AbstractDaemon();
$daemon->name = 'example';
$daemon->setLogger(AbstractDaemon::FILES);

(new DaemonManager($daemon))->handleConsole($argc, $argv);

  class MyDaemon extends AbstractDaemon
  {
      protected function onStart()
      {
          // here's your daemon's code
      }
  }
  

  $daemon = new MyDaemon(AbstractDaemon::TICKABLE, 2); // this daemon will "tick" every 2 seconds
  

  class MyDaemon extends AbstractDaemon
  {
      protected function onTick()
      {
          // here's your daemon's code on every tick
      }
  }
  

  class MyDaemon extends AbstractDaemon
  {
      protected $fp;

      protected function handleStop()
      {
          fclose($this->fp);
          $this->fp = null;
      }

      protected function onStart()
      {
          $this->fp = fopen('example-file', 'r');
          while (is_resource($this->fp)) {
              // data processing
          }
      }
  }
  

class MyDaemon extends AbstractDaemon
{
    protected $config;
    
    protected function onSigUsr1()
    {
        $this->loadConfiguration();
    }
    
    protected function onStart()
    {
        $this->loadConfiguration();
        // ...
    }
    
    protected function loadConfiguration()
    {
        $this->config = json_decode(file_get_contents(__DIR__.'/config.json'));
    }
}

$daemon = new AbstractDaemon();
$daemon->setLogger(AbstractDaemon::SYSLOG);

$daemon = new AbstractDaemon();
$daemon->setLogger(AbstractDaemon::FILES);

$daemon = new AbstractDaemon();
$daemon->setLogger(AbstractDaemon::TERMINAL);
sh
$ php daemon
sh
$ php daemon start
Successfully started. Pid is 8868