php-pmd/daemon 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

php-pmd/daemon

Composer 安装命令:

composer require php-pmd/daemon

包简介

PHP CLI daemon(clio/clio)

README 文档

README

Daemon based on Clio.

The Daemon class provides helpers for starting and killing daemonized processes.

Installation

composer require php-pmd/daemon

PHP 5.4 is required. This library is developed on and is meant to be used on POSIX systems with the posix, pcntl, and sockets extensions loaded.

Daemon::isRunning($pid)

Tests if a daemon is currently running or not. Returns true or false:

<?php
use PhpPmd\Daemon;
if (Daemon::isRunning('/path/to/process.pid')) {
    echo "daemon is running.\n";
} else {
    echo "daemon is not running.\n";
}

Daemon::work(array $options, callable $callable)

Daemonize a $callable callable object. The $options key-value array must contain pid as the path to the PID file:

<?php
use PhpPmd\Daemon;
if (Daemon::isRunning('/path/to/process.pid')) {
    echo "daemon is already running.\n";
} else {
    Daemon::work(array(
            'pid'    => '/path/to/process.pid', // required
            'stdin'  => '/dev/null',            // defaults to /dev/null
            'stdout' => '/path/to/stdout.txt',  // defaults to /dev/null
            'stderr' => '/path/to/stderr.txt',  // defaults to php://stdout
        ),
        function($stdin, $stdout, $stderr) { // these parameters are optional
            while (true) {
                // do whatever it is daemons do
                sleep(1); // sleep is good for you
            }
        }
    );
    echo "daemon is now running.\n";
}

The PID file is an ordinary text file with the process ID as its only content. It will be created by the library automatically if it doesn't exist. It is highly recommended to put a call to sleep to ease the system load.

Daemon::kill($pid, $force = false, $delete = false)

$force

The value of force is true, the sig is SIGKILL

The value of force is false, the sig is SIGTERM

$delete

Flag to delete PID file after killing

Kill a daemonized process:

<?php
use PhpPmd\Daemon;

if (Daemon::isRunning('/path/to/process.pid')) {
    echo "killing running daemon ...\n";
    if (Daemon::kill('/path/to/process.pid')) {
        echo "daemon killed.\n";
    } else {
        echo "failed killing daemon.\n";
    }
} else {
    echo "nothing to kill.\n";
}

If the second parameter is set to true, this function will send SIGKILL to the process.If it is set to false, this function will send SIGTERM to the process.

If the third parameter is set to true, this function will try to delete the PID file after successfully sending the process a kill signal.

Acknowledgments

The text color and style specifiers are taken entirely from PEAR's Console_Color class by Stefan Walk. The Daemon class is heavily inspired from Andy Thompson's blog post on daemonizing a PHP CLI script on a POSIX system.

License

Clio is released under the MIT License.

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固