lacertus has asked for the wisdom of the Perl Monks concerning the following question:

Hey Folks,

I'm greatly hoping someone can indicate what precisely I am doing wrong with the following code. I believe the problem lies in the manner in which my fork() and exec() are interacting. The code is as follows:

sub do_work { my($query) = @_; return if( $query->param('password') ne 'pass' ); if($query->param('which_bot') eq 'chump') { if($pid = fork()) { return; } elsif(defined $pid) { my @args = ("/bin/killall", "-TERM", "python") +; #system(@args); exec('/usr/bin/python /usr/share/chump-1.3/src +/dailychumpbot.py -i -s "neutron.oftc.net" -n chumpy --channel="#iitl +ug" -d /httpd/htdocs/www/chump -e /chump/churn_html.xsl'); exit; } } }


The child process does indeed execute the exec, and a python process begins. Within a second, or so (the script ends shortly after this do_work() function), the python execution ends ... this should not be as it is executing a bot that should be persistent.

Let me give everyone a preemptive thanks for your assitance with this!

Cheers,

Lacertus


------------------------------------
"There is more in heaven and earth
than is dreamt of in your philosophy"

Replies are listed 'Best First'.
Re: CGI fork() Difficulty
by sgifford (Prior) on Oct 26, 2004 at 02:28 UTC
    Try doing a "double-fork" to detach the long-running process from the Web server. Something like:
    if (my $pid1 = fork()) { wait; return; } elsif (defined($pid1)) { # Child if (my $pid2 = fork()) { # Still Child exit(0); } elsif (defined($pid2)) { # Grandchild exec(...); } else { die "Fork error: $!\n"; } } else { die "Fork error: $!\n"; }

    You may also want to do things like POSIX::setsid and setpgrp. Search the archives for daemon for some sample code.