Java Mailing List Archive

http://www.apache-httpd.com/

Home » modperl.perl »

Help -- how to fork an Apache process in mod_perl safely? Not Apache2:Subprocess...

Victor Danilchenko

2009-08-25

Replies: Find Java Web Hosting

Author LoginPost Reply
 Hi all,

 I need to be able to fork an Apache process in daemon form, to do some
housekeeping which might potentially take a few seconds. However, when I
do that, I start getting SQL errors (of the "connection lost" type) in
the browser. I do the fairly standard cleanup to daemonize the child
process, but of course it needs to retain the SQL socket open. Here is
my forking code:

sub modperl_kamikaze_fork () {
  # You will have to do CORE::exit(0) at the end of the execution.

  $SIG{CHLD} = 'IGNORE';
  get_m->flush_buffer;
  defined (my $kid = fork) or die "Cannot fork: $!\n";
  return $kid if $kid;

  my $r = get_r;
  close STDIN; open STDIN, '/dev/null'
 or die "Can't read /dev/null: $!";
  close STDOUT; open STDOUT, '>/dev/null'
 or die "Can't write to /dev/null: $!";
  close STDERR; open STDERR, '>>/tmp/form.log'
 or die "Cannot open /tmp/fork.log\n";
  setsid
 or die "Can't start a new session: $!";

  my $oldfh = select STDERR;
  local $| = 1;
  select $oldfh;
  warn "Child (PID $$) spawned.\n";

  $r->child_terminate;
}


 The Apache2:Subprocess doesn't help me, because I need not to spawn an
external process, but to finish processing in mod_perl context -- just
without bugging the user with it.

 Any ideas on how to either fork better, or how to solve this without
forking (e.g. is there a way to 'append' a function call to the request
after the rest of the request is completed)?

 Many thanks in advance.

--
 Victor Danilchenko
 Senior Software Engineer, AskOnline.net
 victor@(protected)
©2008 apache-httpd.com - Jax Systems, LLC, U.S.A.