2017 © Pedro Peláez
 

library pdaemon

Running PHP script in daemon mode at CLI.

image

consatan/pdaemon

Running PHP script in daemon mode at CLI.

  • Tuesday, February 14, 2017
  • by consatan
  • Repository
  • 1 Watchers
  • 2 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

PDaemon

Running PHP script in daemon mode at CLI. Not available on Windows platforms., (*1)

Install

Requirements
  • PHP >= 5.4
  • pcntl extension
  • posix extension
  • sysvsem extension
Use composer (recommend)
composer require consatan/pdaemon
Download from github
git clone https://consatan.github.com/pdaemon.git

How to use

Simple daemon, (*2)

<?php

require './vendor/autoload.php';

class DemoDaemon extends \Consatan\PDaemon\PDaemon
{
    protected function exec()
    {
        // Your code here.
        echo 'Running in children, PID: ' . posix_getpid() . PHP_EOL;
        // You can access your custom variable in $this->options
        // echo $this->options['your_variable'] . PHP_EOL;
        sleep(3);
    }
}

// Fork 3 children processes, pid file in /var/run/pdaemon.pid
$daemon = new DemoDaemon(['pool' => 3, 'your_variable' => 'hello world']);
$daemon->run();

You can stop daemon process list this, (*3)

<?php

require './vendor/autoload.php';

// replace to your pid path
$pid = file_get_contents('/var/run/pdaemon.pid');

if (false === $pid) {
    // pid file cannot read or not exists.
    exit(-1);
}

$pid = (int)$pid;

if (0 >= $pid) {
    // invalid pid
    exit(-1);
}

// This is a safe terminat function, it will stop process when all children processes stop
posix_kill($pid, SIGTERM);

// You can forced stop the daemon like this: (non-recommended);
// posix_kill($pid, SIGKILL);

Todo

  • [ ] Unit test
  • [ ] Handling reload signal
  • [ ] Handling status signal

The Versions

14/02 2017

dev-master

9999999-dev

Running PHP script in daemon mode at CLI.

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.4
  • ext-pcntl *
  • ext-posix *
  • ext-sysvsem *

 

cli daemon

14/02 2017

1.0.0

1.0.0.0

PHP Daemon, is not available on Windows platforms.

  Sources   Download

BSD 3-Clause

The Requires

  • php >=5.4
  • ext-pcntl *
  • ext-posix *
  • ext-sysvsem *

 

cli daemon