简体   繁体   English

PHP CLI pcntl无法在PHP7.0 Ubuntu xenial服务器上运行

[英]PHP CLI pcntl not working in PHP7.0 Ubuntu xenial server

I'm using this script in PHP 5.5.9: 我在PHP 5.5.9中使用此脚本:

declare(ticks = 1);

pcntl_signal(SIGTERM, array($this, 'stopSending'));
pcntl_signal(SIGINT, array($this, 'stopSending'));
pcntl_signal(SIGUSR1, array($this, 'stopSending'));
pcntl_signal(SIGUSR2, array($this, 'stopSending'));
pcntl_signal(SIGQUIT, array($this, 'stopSending'));
pcntl_signal(SIGHUP, array($this, 'stopSending'));

public function stopSending($signals)
{       
    echo "hello";
    exit();
}

while (true) {
    // some logic
}

In Ubuntu 14 that works fine, but when try to execute in Ubuntu 16.04 with PHP7.0 and try to send signal (kill PID), PHP CLI doesn't stop and keep runnig. 在Ubuntu 14中工作正常,但是当尝试使用PHP7.0在Ubuntu 16.04中执行并尝试发送信号(kill PID)时,PHP CLI不会停止并保持runnig。

In Ubuntu 16.04 i check for pcntl extension, and that is ok: 在Ubuntu 16.04中,我检查了pcntl扩展,这没关系:

>php -m | grep pcntl
pcntl

I do not get any errors when I run, but neither stops (or display echo). 我跑步时没有任何错误,但都没有停止(或显示回声)。

Is there a problem with PHP7 and pcntl? PHP7和pcntl有问题吗?

UPDATE UPDATE

The problem is when encapsulate the while loop into function: 问题是将while循环封装到函数中时:

function start()
{
    while (true) {
        // some logic
    }
}

declare(ticks = 1);

pcntl_signal(SIGTERM, "stopSending");
pcntl_signal(SIGINT, "stopSending");
pcntl_signal(SIGUSR1, "stopSending");
pcntl_signal(SIGUSR2, "stopSending");
pcntl_signal(SIGQUIT, "stopSending");
pcntl_signal(SIGHUP, "stopSending");

function stopSending($signals)
{       
    echo "hello";
    exit();
}

start();

This code does not stop. 此代码不会停止。

There is a good explanation about PHP signal handling here . 有一个关于PHP处理信号,很好地解释这里 So the best way to make sure your signal handler triggers in appropriate time would be something like this: 因此,确保您的信号处理程序在适当的时间触发的最佳方法是这样的:

<?php

declare(ticks = 1);

function start()
{
    while (true) {
        pcntl_signal_dispatch();
    }
}

pcntl_signal(SIGTERM, "stopSending");
pcntl_signal(SIGINT, "stopSending");
pcntl_signal(SIGUSR1, "stopSending");
pcntl_signal(SIGUSR2, "stopSending");
pcntl_signal(SIGQUIT, "stopSending");
pcntl_signal(SIGHUP, "stopSending");

function stopSending($signals)
{       
    echo "hello";
    exit();
}

start();

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM