Mailing List Archive

use of STDERR in forked children
Hi, I'm working on a mechanism to fork a a long
running child process. I've followed the standard
mod_perl forking wisdom in
http://modperlbook.org/html/ch10_02.html.

My issue is that warn() doesn't work in the child
process, though printing directly to STDERR does.

Also, I want STDERR to go to the apache log like it
normally would, but I don't know how to determine what
the path to the log is. I can't hard code it since it
changes depending on the environment (dev vs. qa vs.
prod).

I haven't tried this using Apache::Registry (since we
don't run it) but I thought it might be and Embperl
thing since I couldn't find anything on the mod_perl
lists...

I'm using Apache 1.3 and Embperl 1.36

Your help is greatly appreciated.

Here's the code:
[# SNIP #]
[!
use POSIX 'setsid';
use Apache;
!]
[.-
my $childPid = undef;
$SIG{CHLD} = 'IGNORE';

if ($childPid = fork) {
$output = "Parent $$ has finished, kid's PID:
$childPid\n";
}
else {
warn "starting child $$\n";
$req_rec->cleanup_for_exec( ); # untie the
socket
chdir '/' or die "Can't chdir
to /: $!";
open STDIN, '/dev/null' or die "Can't read
/dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write
to /dev/null: $!";
open STDERR, '>>/tmp/log' or die "Can't write
to /tmp/log: $!";
setsid or die "Can't start
a new session: $!";

my $oldfh = select STDERR;
local $| = 1;
select $oldfh;

# ends up in /tmp/log
print STDERR "Where do the children [$$]
play?\n";

# ends up nowhere
warn "Where do the children [$$] play?\n";

# do something time-consuming
for (1..20) {
sleep 1;
warn "$_\n";
}
warn "completed $$\n";

CORE::exit(0); # terminate the process
}
-]
[- $dbgLogLink = 0; -]
<html><head><body>
<xmp>[+ $output +]</xmp>
</body></html>
[# ENDSNIP #]

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org
Re: use of STDERR in forked children [ In reply to ]
On 6/28/06, Matthew Smith <matthewi33@yahoo.com> wrote:
>
> Hi, I'm working on a mechanism to fork a a long
> running child process. I've followed the standard
> mod_perl forking wisdom in
> http://modperlbook.org/html/ch10_02.html.
>
> My issue is that warn() doesn't work in the child
> process, though printing directly to STDERR does.


You probably want to reset/check $SIG{__WARN__} .

Also you probably want to reset $SIG{PIPE}, from memory.

Also, I want STDERR to go to the apache log like it
> normally would, but I don't know how to determine what
> the path to the log is. I can't hard code it since it
> changes depending on the environment (dev vs. qa vs.
> prod).


I thought STDERR in the apache process already went to the logfile? In
which case, just don't change it in your child..

--
- Gus