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

In reply to Re: Fork child by ig
in thread Fork child by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.