You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Aaron Johnson <so...@gina.net> on 2002/10/31 05:18:03 UTC

Can't make a clean break (fork)

Back again with a similar question.  I put in place some code a few
months ago based on a reply from a member.  It is allowing me to run the
external process correctly, but in doing deployment of the code and
watching the system memory usage it appears that the process is still
attached to the mod_perl process.  Here is a snippet of code:

   $SIG{'CHLD'} = 'IGNORE';
   defined (my $kid = fork) or 
   $seo->error_to_log( "Cannot fork: $!" );
   if ($kid) {
       $seo->error_to_log( "Parent has finished, kid's PID: $kid" );
   } else {        
            
   	close($req_rec->connection->fileno);
        # added in an attempt to make the process completely seperate
        $req_rec->cleanup_for_exec(); # untie the socket
        # chdir '/' or die "Can't chdir to /: $!";
        close STDIN;
        close STDOUT;
        close STDERR;        
           
        $seo->_debug(0);
        exec( $seo->external_report_app(), $number ) or
        $seo->error_to_log("Cannot execute exec: $!" , 1 );
        CORE::exit(0);
    }    

I am running this with the latest version of mod_perl and Apache and the
dev3 of 2.0b9 with 5.8 Perl on the development server and 5.6.1 and
2.0b8 on a production machine, both running Linux.
My detached process starts approx. 16 additional forked processes so it
is important for me to complete detach myself from the apache process.

Aaron Johnson


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: Can't make a clean break (fork)

Posted by erik <er...@creditminders.com>.
On Wed, Oct 30, 2002 at 11:18:03PM -0500, Aaron Johnson wrote:
> Back again with a similar question.  I put in place some code a few
> months ago based on a reply from a member.  It is allowing me to run the
> external process correctly, but in doing deployment of the code and
> watching the system memory usage it appears that the process is still
> attached to the mod_perl process.  Here is a snippet of code:
>    	close($req_rec->connection->fileno);
>         # added in an attempt to make the process completely seperate
>         $req_rec->cleanup_for_exec(); # untie the socket
>         # chdir '/' or die "Can't chdir to /: $!";
>         close STDIN;
>         close STDOUT;
>         close STDERR;        

You need a POSIX::setsid() in there, too.  That will disassociate you
from the parent process' session, tty, etc.  The chdir / is also a
good idea.

here's the sequence I use, setting $0 is optional:

    chdir "/";
    $0=$name;

    close STDOUT;
    close STDERR;
    close STDIN;

    POSIX::setsid();   # this takes care of controlling terminals




Erik


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


RE: Can't make a clean break (fork)

Posted by Jordan Baker <su...@rogers.com>.
I'm currently doing something similar with fork in a mod_perl app for
doing some customer e-mailing which takes a too long to keep in
foreground.

Considering writing a little script to call via system() that will
fork() and perform the tasks because they can be done asynchronously.

Thought I would float out the idea and see what you thought about that.
______________________
Jordan Baker
jbb@contradix.comĀ 


-----Original Message-----
From: Aaron Johnson [mailto:solution@gina.net] 
Sent: October 30, 2002 11:18 PM
To: embperl@perl.apache.org
Subject: Can't make a clean break (fork)

Back again with a similar question.  I put in place some code a few
months ago based on a reply from a member.  It is allowing me to run the
external process correctly, but in doing deployment of the code and
watching the system memory usage it appears that the process is still
attached to the mod_perl process.  Here is a snippet of code:

   $SIG{'CHLD'} = 'IGNORE';
   defined (my $kid = fork) or 
   $seo->error_to_log( "Cannot fork: $!" );
   if ($kid) {
       $seo->error_to_log( "Parent has finished, kid's PID: $kid" );
   } else {        
            
   	close($req_rec->connection->fileno);
        # added in an attempt to make the process completely seperate
        $req_rec->cleanup_for_exec(); # untie the socket
        # chdir '/' or die "Can't chdir to /: $!";
        close STDIN;
        close STDOUT;
        close STDERR;        
           
        $seo->_debug(0);
        exec( $seo->external_report_app(), $number ) or
        $seo->error_to_log("Cannot execute exec: $!" , 1 );
        CORE::exit(0);
    }    

I am running this with the latest version of mod_perl and Apache and the
dev3 of 2.0b9 with 5.8 Perl on the development server and 5.6.1 and
2.0b8 on a production machine, both running Linux.
My detached process starts approx. 16 additional forked processes so it
is important for me to complete detach myself from the apache process.

Aaron Johnson


---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org