in reply to Re^4: CGI and Background Processes
in thread CGI and Background Processes

You need to reopen them to somewhere, e.g. something like

... sub background_run { my @cmd = @_; if (my $pid = fork) { # parent # (...) } elsif (defined $pid) { # child # This redirects both STDOUT/STDERR to the same logfile. # Instead of appending (">>"), you could of course also # create the file anew every time (">"), or create two # separate logfiles, or whatever... my $logfile = "/tmp/bgrun.log"; # re-open STDERR/STDOUT (closes first implicitly) # (if this fails, the error msg will end up in Apache's error_ +log) open STDERR, ">>", $logfile or die "Couldn't open $logfile: $! +"; open STDOUT, ">>", $logfile or die "Couldn't open $logfile: $! +"; # optional # my $app_path = "/path/to/dir/to/run/long-running-cgi/in"; # chdir $app_path or die "Couldn't chdir() to $app_path: $!"; exec @cmd; die "Couldn't exec '@cmd': $!"; } else { die "Couldn't fork: $!"; } } ... # files (perl, sleep.pl) must be found - in case of doubt, # use absolute paths... background_run("perl", "./sleep.pl"); ...

Replies are listed 'Best First'.
Re^6: CGI and Background Processes
by mrguy123 (Hermit) on May 05, 2008 at 16:05 UTC
    Thanks, this works great!!
    I still need to apply it to my full CGI program (I tested it successfully on my dummy program) but I'm sure it will be fine
    Thanks for your help and patience (and teaching me a few new things)
    Guy