PHP code example of consatan / pdaemon

1. Go to this page and download the library: Download consatan/pdaemon 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/ */

    

consatan / pdaemon example snippets




 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();



place 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);