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

OK, 5 hours later and I'm stumped...

I need to run a program from a perl CGI script. The invoked program must be completely severed from the original program, thus allowing the output of the original program to go to the client browser while the invoked program does it's ridiculously long task. I've tried lots of stuff, 'fork && exit;', 'use POSIX qw(setsid)', 'system (cmd &)' and even tried experimenting with open and pipes.

Here is the code:

PROGRAM1:
#/usr/bin/perl # program1.cgi is invoked by a web browser # print <<EOF; Content-type: text/html <html> <body> this output should come up while program2.cgi is still thinking </body> </html> EOF # at this point, we want to invoke program2.cgi and terminate program1 +.cgi exit 1;
PROGRAM2:
#/usr/bin/perl # program2.cgi does something incredibly long but has no output to STD +OUT, so we would like this sucker to run independently of program1.cg +i. sleep 200; exit 1;
any help will be appreciated most sincerely, f.

Replies are listed 'Best First'.
Re: Stumped on IPC...
by suaveant (Parson) on Sep 25, 2001 at 22:25 UTC
    when you fork && exit, did you close STDERR STDOUT and STDIN? You must close STDOUT... simply not writing to it is not enough

                    - Ant
                    - Some of my best work - Fish Dinner

      That was it... thanks. Magic code is as follows:
      ... fork && exit; close STDOUT; system ("./someprog.cgi") || warn "funniness $!"; ...
      thanks s.a.
      f.
        Watch out for how you check the return from system. It returns 0 on success, not failure, in a typical environment.
        my $result = system("./someprog.cgi"); warn "funniness: error code $?" if $result;

        buckaduck

Re: Stumped on IPC...
by ismail (Acolyte) on Sep 25, 2001 at 22:24 UTC
    Environment info... this is on a Linux box running Perl 5.6.