Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

This question is also posted on StackOverflow

I have this very simple Perl script on my linux server.

What I would like to be able to do is to call the script from a browser on a separate machine
Have the script initiate a fork ->
Have the parent send an httpResponse -> (freeing up the browser)
Immediately end the parent ->
Allow the child to do its job, heavy complex database work, which could take a minute or two ->
Have the child end itself with no output whatsoever

When I call this script from a browser, the browser does not receive the sent response till the child is complete.

Yes, it works when called from the command line.

Is what I want to do possible?
p.s. I even tried it with Proc::Simple->start, but I get the same hangin up.

#!/usr/bin/perl local $SIG{CHLD} = "IGNORE"; use lib '/var/www/cgi-bin'; use CGI; my $q = new CGI; if(!defined($pid = fork())) { die "Cannot fork a child: $!"; } elsif ($pid == 0) { print $q->header(); print "i am the child\n"; sleep(10); print "child is done\n"; exit; } else { print $q->header(); print "I am the parent\n"; print "parent is done\n"; exit 0; } exit 0;

In reply to Can I have a Perl script, initiated from a browser, fork itself, and not wait for the child to end? by bartender1382

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.