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

I use 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 } 

On Ubuntu 14 this works fine, but when you try to run on Ubuntu 16.04 with PHP7.0 and try to send a signal (kill PID), the PHP CLI does not stop and continues to work.

On Ubuntu 16.04, I check the pcntl extension, and this is normal:

 >php -m | grep pcntl pcntl 

I do not get any errors at startup, but it does not stop (or displays an echo).

Is there a problem with PHP7 and pcntl?

UPDATE

The problem is to encapsulate the while loop in the 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.

+5
source share
1 answer

There is a good explanation of PHP signal processing here . Thus, the best way to make sure your signal handler starts at the 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(); ?> 
+2
source

All Articles