[asterisk-users] AGI and forking

A J Stiles asterisk_list at earthshod.co.uk
Wed Apr 13 08:08:03 CDT 2011


Hi.  I just want to make sure I understand this before doing something that 
might break things spectacularly for our users and customers  :)

We are using Asterisk 1.6.2.9 and my programming language of choice is Perl.

I want, when a call comes in on someone's DDI number  (which the person who 
dialled it can only possibly have obtained by dialling 1471 after we called 
them),  to be able to look up the caller's details from one of our databases  
(where the number ought to be stored, because we already dialled it).

Now, this search is going to take some time; so I'd like for the AGI script to 
fork a clone of itself, so the parent process can exit and the dialplan 
continue on to ring the person's phone, while the database lookup is done in 
the background  (the script doesn't need to have any further contact with 
Asterisk -- it will initiate any necessary future communication via other 
channels).


Is this the sort of thing I need?

##########  begin code snippet  ##########

#!/usr/bin/perl -w
use strict;
use Asterisk::AGI;

my $AGI = new Asterisk::AGI;
my %params = $AGI->ReadParse();

$SIG{CHLD} = "IGNORE";

if (my $child_pid = fork) {
    #  This is executed in the parent process
    exit;
}
elsif (defined $child_pid) {
    #  This is executed in the child process

    close STDIN;
    close STDOUT;
    close STDERR;

    #  Load some more modules and do some stuff
    #   that will take a long time

    exit;
}
else {
    die "Could not fork: $!";
};

##########  end code snippet  ##########

Am I right in thinking I shouldn't have to worry about zombie processes, 
because the parent exits before the child and the init in modern Linux 
distros is smart enough to deal with orphaned processes itself?

-- 
AJS

Answers come *after* questions.



More information about the asterisk-users mailing list