Problem with signals

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rebel7284
Forum Newbie
Posts: 7
Joined: Mon Oct 20, 2008 2:59 pm

Problem with signals

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with signals

Post 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);
}
?>
Rebel7284
Forum Newbie
Posts: 7
Joined: Mon Oct 20, 2008 2:59 pm

Re: Problem with signals

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with signals

Post 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:
Last edited by requinix on Tue Oct 21, 2008 3:06 pm, edited 1 time in total.
Rebel7284
Forum Newbie
Posts: 7
Joined: Mon Oct 20, 2008 2:59 pm

Re: Problem with signals

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with signals

Post 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.
Last edited by requinix on Tue Oct 21, 2008 5:00 pm, edited 1 time in total.
Rebel7284
Forum Newbie
Posts: 7
Joined: Mon Oct 20, 2008 2:59 pm

Re: Problem with signals

Post by Rebel7284 »

tasairis wrote:Wtf :|

Code: Select all

php -f test.php
Tried the -f option. Same behavior.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Problem with signals

Post by Syntac »

Have you tried it with the -q option?
Rebel7284
Forum Newbie
Posts: 7
Joined: Mon Oct 20, 2008 2:59 pm

Re: Problem with signals

Post 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?)
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Problem with signals

Post by Syntac »

Er... Never mind. For a moment there I thought -q changed something to do with the execution mode. :roll:
Rebel7284
Forum Newbie
Posts: 7
Joined: Mon Oct 20, 2008 2:59 pm

Re: Problem with signals

Post by Rebel7284 »

bump
Post Reply