Apache CGI Timeout - how does it kill and/or notify the child?
I have some potentially long lived CGI applications which must clean up their environment regardless of whether they complete normally or if they're killed by Apache because they're taking too long. They're using shared memory so I can't rely on the operating system's normal process cleanup mechanisms.
How does Apache kill its CGI children when they're timing out? I can't find any documentation or specification for how its done, nor whe开发者_JS百科ther its possible for the child to intercept that so it can shut down cleanly.
I could not find any official Apache documentation on this, but the following script shows that CGI scripts are sent SIGTERM
on timeout, not SIGKILL
(at least in my version of Apache, 2.2.15):
#!/usr/bin/perl
use strict;
use warnings;
use sigtrap 'handler' => \&my_handler, 'normal-signals';
use CGI;
sub my_handler {
my ($sig) = @_;
open my $fh, ">", "/var/www/html/signal.log" or die $!;
print $fh "Caught SIG$sig";
close $fh;
}
sleep 10 while 1;
Output:
Caught SIGTERM
Nop, Apache send kill signal and this signal can not be caught or handled. So signal handler do nothing in this case.
looks like apache doesn't do anything? I just added a signal handler to one of my perl cgi scripts that Apache timed out on, and I get nothing :(
bit of a shame really.
Note that in case these tasks are really taking too long and there isn't really a reply expected by the client, you could instead start a background process on your server whenever you receive such a request.
This of course means that you probably want to make sure you don't start the background process more than a certain number of times (possibly just once) and you can have that process save information in a file or shared memory so the client can check progress.
Not allowing the background process from being started too many times will save your server memory / CPU... otherwise it will become unresponsive.
And that way you do not have to worry too much about Apache killing your long process since there is no more timeout concerns with it.
精彩评论