Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Closing parent CGI after fork()

by hambo (Novice)
on May 04, 2004 at 11:53 UTC ( #350294=perlquestion: print w/replies, xml ) Need Help??
hambo has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
Want to know about CGI. I have a script, which creates a web page using Simple enough. Now, inside this, it should fork another perl script, called, which should sleep until I send it a SIGALRM. This all works, but the web page continues to have the hour glass (like the page is waiting for input from the CGI) until I send my sleeping process the SIGALRM. How would I get the web page to do the complete job (i.e. not wait with the hourglass) while still running the in the background?

#!/usr/bin/perl -w #Create initial feedback page use CGI ':standard'; my $query=new CGI; print $query->header("text/html"); print $query->start_html(-title => "This is my first web page generate +d via CGI"); print $query->h1('This is the header document'); print <<EOF; This is the body of the document<br/> What goes in here is really anything you wish to put in here<br/> EOF if ( my $pid = fork() ) { system ("./"); exit; # This current process } else { print $query->end_html; }

My is simple:
#!/usr/bin/perl -w sleep; exit;
I know little about CGI, so this is all a bit new to me.

20040504 Edit by broquaint: Changed title from 'CGI question - getting rid of the hour glass'

Replies are listed 'Best First'.
Re: Closing parent CGI after fork()
by tinita (Parson) on May 04, 2004 at 11:57 UTC
    try: close STDOUT; before the system call.

    update: just trying it out and i'm not getting it to work, but i remember it used to work by closing STDOUT... update2: works. you might also want to do exec() instead of system().

      You probably want to close STDIN and STDERR too

        You probably want to close STDIN and STDERR too

        You probably don't want to do any of this stuff, but you'll find you have to... :(

        Even though the valuable information found by following links to merlyn's nodes/articles elsewhere in this thread only talk about closing STDOUT, I found I had to close STDIN and STDERR, as well. This is for Apache 2. I had to jump through some hoops to redirect the child's STDERR to a file and later send anything found there to the apache error_log, rather than lose any STDERR output produced by the child... a bit ugly, but it works.

        Also, I found I had to reopen all 3 filehandles (to/from /dev/null for STDOUT and STDIN, to a file for STDERR) as well, since my child is opening other files, and if you just close STD(IN|OUT|ERR), the next time you open a file it will get file descriptor 0, 1, or 2, which is essentially reopening STD(IN|OUT|ERR), and I found this causes apache to hang again! Ugh. forker beware!



Re: Closing parent CGI after fork()
by b10m (Vicar) on May 04, 2004 at 13:03 UTC
Re: Closing parent CGI after fork()
by pelagic (Priest) on May 04, 2004 at 13:02 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://350294]
Approved by pelagic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2017-05-24 19:44 GMT
Find Nodes?
    Voting Booth?