Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 whether its possible for the child to intercept that so it can shut down cleanly.

like image 334
Alnitak Avatar asked Sep 06 '25 03:09

Alnitak


2 Answers

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
like image 65
ThisSuitIsBlackNot Avatar answered Sep 07 '25 22:09

ThisSuitIsBlackNot


Nop, Apache send kill signal and this signal can not be caught or handled. So signal handler do nothing in this case.

like image 43
Muhammad Razib Avatar answered Sep 07 '25 22:09

Muhammad Razib