7

I'm using this script in 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.

In Ubuntu 16.04 i check for pcntl extension, and that is ok:

>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?

UPDATE

The problem is when encapsulate the while loop into function:

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.

chridam
  • 100,957
  • 23
  • 236
  • 235
jfra
  • 126
  • 8
  • The code you posted does not exit on SIGTERM. So it shouldn't stop when sending SIGTERM to it. Why it didn't work before, is the right question. – Erki Aring Aug 02 '16 at 10:15
  • @ErkiA I edited the code. I have a for loop after the register signal handlers. The problem is that signals are captured by stopSending(), but not printing echo. – jfra Aug 02 '16 at 11:06
  • @ErkiA i mean, that is debug code. The original code of function "stopSending" exit the program. – jfra Aug 02 '16 at 11:36
  • 2
    Could you please try to create a minimal complete example which demonstrates the problem. Currently you have shown some fragments of the code, which I think are not relevant. Probably, if you try to recreate the problem with minimal code, you'll find the cause of your problem already. – Erki Aring Aug 02 '16 at 12:13
  • 1
    This works fine for me with PHP 7.0.9. – miken32 Aug 03 '16 at 20:58
  • @ErkiA I updated the code. If wrap the loop into function does not stop. – jfra Aug 09 '16 at 08:28
  • @miken32 I updated the code. – jfra Aug 09 '16 at 08:31
  • 1
    Move declare statement to the beginning of the file and add some logic to your while loop, "usleep(0)" for example. – Erki Aring Aug 09 '16 at 09:08
  • @ErkiA it works! thx! any idea why is that? – jfra Aug 09 '16 at 09:16

1 Answers1

4

There is a good explanation about PHP signal handling here. 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();

?>
Community
  • 1
  • 1
Erki Aring
  • 2,032
  • 13
  • 15