Page 1 of 1

Problem with signals

Posted: Mon Oct 20, 2008 3:11 pm
by Rebel7284
Hi,
I have a problem with signal handling not working in cli PHP

Relevant information:
PHP 4.3.11 (cli) (built: Jun 21 2005 12:10:12)
Cent OS kernel 2.6.9

Sample code:

Code: Select all

 
<?
pcntl_signal(SIGINT, "sighandler");
pcntl_signal(SIGQUIT, "sighandler");
 
 
function sighandler($signo){
    echo "Got ", $signo;
    //ob_end_flush();
}
 
while(1){
    sleep(5);
}
?>
With this code running, I can press Ctrl-C and Ctrl-\ as much as I want, it ignores it and just sits there.

Does anyone have any idea what I'm doing wrong?

Re: Problem with signals

Posted: Mon Oct 20, 2008 4:08 pm
by requinix
You have to use ticks as well.

Code: Select all

<?
declare(ticks = 1);
pcntl_signal(SIGINT, "sighandler");
pcntl_signal(SIGQUIT, "sighandler");
 
function sighandler($signo){
    echo "Got ", $signo;
    //ob_end_flush();
}
 
while(1){
    sleep(5);
}
?>

Re: Problem with signals

Posted: Mon Oct 20, 2008 4:37 pm
by Rebel7284
With ticks, the signals are not ignored. However, the sighandler function is not executed. The program terminates as if the signal handler was never set.

Re: Problem with signals

Posted: Mon Oct 20, 2008 6:04 pm
by requinix
Rebel7284 wrote:With ticks, the signals are not ignored. However, the sighandler function is not executed. The program terminates as if the signal handler was never set.
I tried that exact same code and it worked fine for me.

Code: Select all

<?
 
error_reporting(E_ALL);
ini_set("display_errors", 1);
 
declare(ticks = 1);
// ...
If you don't get any clues from running that post your PHP version.
Ah, he did already :banghead:

Re: Problem with signals

Posted: Tue Oct 21, 2008 8:46 am
by Rebel7284
tasairis wrote: I tried that exact same code and it worked fine for me.

Code: Select all

<?
 
error_reporting(E_ALL);
ini_set("display_errors", 1);
 
declare(ticks = 1);
// ...
If you don't get any clues from running that post your PHP version.
Tried it with those extra lines. No output whatsoever

Code: Select all

 
[dmitriyb@nydev_web ~]$ php test.php   // Ctrl-C pressed here, seems to result in a newline and termination
 
[dmitriyb@nydev_web ~]$ php test.php   // Ctrl-\ pressed here, results in 'Quit' and termination
Quit
[dmitriyb@nydev_web ~]$
 
I posted the PHP version in the first post :)
PHP 4.3.11 (cli) (built: Jun 21 2005 12:10:12)
Sadly updating is not an option, we have a ridiculous amount of legacy code.

This is driving me nuts.

Re: Problem with signals

Posted: Tue Oct 21, 2008 3:14 pm
by requinix
Wtf :|

Code: Select all

php -f test.php
Hmm... it was working with -f and not without, but now it works with both... Nevermind the above.

Re: Problem with signals

Posted: Tue Oct 21, 2008 3:38 pm
by Rebel7284
tasairis wrote:Wtf :|

Code: Select all

php -f test.php
Tried the -f option. Same behavior.

Re: Problem with signals

Posted: Tue Oct 21, 2008 3:41 pm
by Syntac
Have you tried it with the -q option?

Re: Problem with signals

Posted: Tue Oct 21, 2008 4:01 pm
by Rebel7284
Syntac wrote:Have you tried it with the -q option?
I have now, no change in script's behavior

(don't know why it would make a difference though, doesn't -q _suppress_ warnings which I would want to see if there were any?)

Re: Problem with signals

Posted: Tue Oct 21, 2008 5:32 pm
by Syntac
Er... Never mind. For a moment there I thought -q changed something to do with the execution mode. :roll:

Re: Problem with signals

Posted: Wed Oct 22, 2008 4:10 pm
by Rebel7284
bump