SEGINT, readline extension & clean script exit - how to do it
| From: | Jochem Maas | Date: | Tue, 28 Nov 2006 13:25:58 +0000 |
| Subject: | SEGINT, readline extension & clean script exit - how to do it | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-245226@lists.php.net to get a copy of this message | ||
hi people,
I have a script - if I run it and then hit CTRL+C the script dies
and I'm returned to the prompt, only this leads to an unclean shutdown
and the directory one was in when the script was runs will contain a
file named 'session_mm_cli0.sem' - this quikly becomes messy.
so I went looking for a solution that allows me to trap SIGINT (which is the
signal that is produced by CTRL+C) and exit the script gracefully...
I searched the manual and came up with the following (which is more or
less a verbatim copy of what the example in the manual), which I
place at the top of my script:
<?php
// signal handler function
declare (ticks = 1);
function sig_handler($signo) {
switch ($signo) {
case SIGTERM:
case SIGSEGV:
case SIGQUIT:
case SIGABRT:
case SIGINT:
exit;
}
}
// setup signal handlers
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGSEGV, "sig_handler");
pcntl_signal(SIGQUIT, "sig_handler");
pcntl_signal(SIGTERM, "sig_handler");
pcntl_signal(SIGINT, "sig_handler");
?>
now this does trap the SIGINT and allow for clean shutdown, but it has an
annoying side-affect: if readline() has been called and is waiting for input
the signal won't be handled until readline returns (i.e. until it recieves a newline char)
at which point thew script does die gracefully;
without the use of pcntl_signal() the SIGINT causes immediate script death regardless of
whether readline() is waiting for output.
so the question is how can I use readline() and handle the SIGINT gracefully whilst not waiting
for readline() to return control to my script?
thanks in advance!
rgds,
Jochem