in reply to Fork child

Update: I added a second write in the second child, after closing STDOUT in the child. The output to STDERR is still written to the error log.

I was quite surprised that output from a child process, to both STDOUT and STDERR and long after the parent process had exited, was included in the response and written to the error log. This was on Apache on Linux (CentOS 5.2).

#!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); print header, start_html(-title=>'fork test',); print "Parent start at " . localtime() . "<br>\n"; print STDERR "Parent start at " . localtime() . "\n"; if(my $pid = fork()) { sleep(5); print "Parent forking at " . localtime() . "<br>\n"; print STDERR "Parent forking at " . localtime() . "\n"; wait(); } else { print "First child at " . localtime() . "<br>\n"; print STDERR "First child at " . localtime() . "\n"; exit(0); } if(my $pid = fork()) { print "Parent forking again at " . localtime() . "<br>\n"; print STDERR "Parent forking again at " . localtime() . "\n"; } else { sleep(50); print "Second child at " . localtime() . "<br>\n"; print STDERR "Second child at " . localtime() . "\n"; close(STDOUT); sleep(50); print STDERR "Second child after STDOUT close at " . localtime +() . "\n"; exit(0); } print end_html; print "Parent exiting at " . localtime() . "<br>\n"; print STDERR "Parent exiting at " . localtime() . "\n"; exit(0);

This produced the following in the browser:

Parent start at Thu Dec 4 09:03:33 2008 First child at Thu Dec 4 09:03:33 2008 Parent forking at Thu Dec 4 09:03:38 2008 Parent forking again at Thu Dec 4 09:03:38 2008 Parent exiting at Thu Dec 4 09:03:38 2008 Second child at Thu Dec 4 09:04:28 2008

And the following in the error log

[Thu Dec 04 09:03:33 2008] [error] [client 127.0.0.1] Parent start at +Thu Dec 4 09:03:33 2008 [Thu Dec 04 09:03:33 2008] [error] [client 127.0.0.1] First child at T +hu Dec 4 09:03:33 2008 [Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent forking a +t Thu Dec 4 09:03:38 2008 [Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent forking a +gain at Thu Dec 4 09:03:38 2008 [Thu Dec 04 09:03:38 2008] [error] [client 127.0.0.1] Parent exiting a +t Thu Dec 4 09:03:38 2008 [Thu Dec 04 09:04:28 2008] [error] [client 127.0.0.1] Second child at +Thu Dec 4 09:04:28 2008 [Thu Dec 04 09:04:28 2008] [error] [client 127.0.0.1] File does not ex +ist: /var/www/html/favicon.ico, referer: http://localhost/cgi-bin/tes +t.pl [Thu Dec 04 09:05:18 2008] [error] [client 127.0.0.1] Second child aft +er STDOUT close at Thu Dec 4 09:05:18 2008

Replies are listed 'Best First'.
Re^2: Fork child
by gayathriAthreya (Novice) on Dec 03, 2008 at 20:31 UTC
    The child I create does not write to the error log for some reason. Putting an eval around the code helps a great deal if you catch the error and immediately email the person who made the request. But I am going to redirect stderr to another log file and I think that will work. Thanks a lot.

      We would need more information about your server configuration and CGI script to determine why output to STDERR doesn't end up in the server's error log. If you want to pursue this, you should let us know your operating system, whether you are using mod_perl, fastCGI, etc. and versions of all the major components. And posting a small test script that exhibits the behaviour in your enviornment would be very helpful. You might also run the script I provided on your server and let us know what happens.

      On the other hand, if writing to another log file meets your needs, that's fine too.

        I was able to redirect STDERR and STDOUT to a log file. Great!! Thanks for all your help!