Apache kills CGI script after redirecting or closing STDOUT

Apache kills CGI script after redirecting or closing STDOUT

Post by John Boynt » Tue, 11 Jun 2002 23:25:05



I'm working with apache 1.3.22 on Sun0S5.8 and believe apache is
killing my perl CGI script whenever it redirects or closes STDOUT.  I
need to close STDOUT in a child process that I'm intending to create.
I've created some sample code that captures this problem.  When apache
runs the following code as a CGI script, my web browser displays
"Hellooooo how are you?" but the die error message never makes it to
the log file.  If I run this script from the command line I get the
both the greeting and the die error message.  If I comment out
the line 'close STDOUT' and launch it from a web browser the die error
message is displayed in the log file.

#!/usr/bin/perl

select((STDOUT, $|=1)[0]);  #flush STDOUT writes immediately!
print "Content-type: text/html\r\n\r\n";
print "Hellooooo how are you?\n";

if ($pid != fork) {

        close STDOUT;
        die "ERROR: $!";

Quote:}

Thanks for your help in advance,
John
 
 
 

Apache kills CGI script after redirecting or closing STDOUT

Post by David Efflan » Sat, 15 Jun 2002 12:46:20



Quote:> I'm working with apache 1.3.22 on Sun0S5.8 and believe apache is
> killing my perl CGI script whenever it redirects or closes STDOUT.  I
> need to close STDOUT in a child process that I'm intending to create.
> I've created some sample code that captures this problem.  When apache
> runs the following code as a CGI script, my web browser displays
> "Hellooooo how are you?" but the die error message never makes it to
> the log file.  If I run this script from the command line I get the
> both the greeting and the die error message.  If I comment out
> the line 'close STDOUT' and launch it from a web browser the die error
> message is displayed in the log file.

> #!/usr/bin/perl

> select((STDOUT, $|=1)[0]);  #flush STDOUT writes immediately!
> print "Content-type: text/html\r\n\r\n";
> print "Hellooooo how are you?\n";

> if ($pid != fork) {

>    close STDOUT;
>    die "ERROR: $!";
> }

There may be more to it than that.  Search for daemonize in 'perldoc
perlipc' and read through that section.

--
David Efflandt - All spam ignored  http://www.de-srv.com/
http://www.autox.chicago.il.us/  http://www.berniesfloral.net/
http://cgi-help.virtualave.net/  http://hammer.prohosting.com/~cgi-wiz/

 
 
 

1. Does Apache kills CGI-scripts after close(STDOUT) ?

Dear friends,

I would like to use Apache/1.3.3 (Unix) Debian/GNU and a simple Perl
CGI-script to log the clicks at my pages into a MySQL database. So I
first send the redirection headers, then close(STDOUT) to make the
things faster and the execute the SQL-statement.

However the values in the database get incremented only if I comment
that close statement (please see below) out. Does anyone know, why?

Regards
Alex

#!/usr/local/bin/perl -T
use strict;
use DBI;

################################################################################
# First print response and close descriptors                                   #
################################################################################

my $url;                                         # the URL passed in PATH_INFO

die unless $url = $ENV{PATH_INFO};               # there is a / at the beginning
$url .= $ENV{QUERY_STRING};

$|++;                                            # disable buffering

print "Status: 302 Found
Uri: http:/$url
Location: http:/$url
Content-type: text/html

<HTML><BODY><A HREF=\"http:/$url\">http:/$url</A></BODY></HTML>";

#close (STDOUT);                                 # DOES NOT WORK IF NOT COMMENTED
#close (STDERR);
#close (STDIN);

################################################################################
# Set alarm and connect to the database                                        #
################################################################################

my $dbh;                                         # database handle

$dbh =  DBI -> connect ($MYSQL, $USER, $PASS, {RaiseError => 0, AutoCommit => 1}
$dbh -> do ('update GAZETI set hits = hits + 1 where url = ' .
            $dbh -> quote ($ENV{QUERY_STRING}));
$dbh -> disconnect;

2. Simple command won't run in Crontab script

3. apache/mod_perl/cgi:Why doesn't the script terminate when the browser closes connection?

4. Year 2000

5. Redirect cgi (perl) scripts using Apache 0.8.14

6. How do I change colour-depth/resolution

7. Linux/Apache - my cgi-bin script can't write/redirect files in any directory

8. What's Here for the Lowly Speaker?

9. Apache, nph- cgi scripts not getting killed..

10. redirecting STDOUT of a bg process to STDOUT of a new xterm

11. file descriptor Q, redirecting stderr and stdout to stdout and file.

12. Closing stdout and stderr, reopening them to a file within a script.

13. How to close stdout from shell script