jeffa has asked for the wisdom of the Perl Monks concerning the following question:

I am testing out some daemon code for a Linux platform. Simple stuff.

The following snippet handles two signals:
INT - close open filehandles and exit
HUP - print a message to a log file

My problem is that I can't seem to access STDOUT, or any file handle that I tie to my log file. It seems that the only descriptor I can get any results from is STDERR.

Here's the code:

use strict; use POSIX; my $die = 0; my $pid = fork; die "no fork\n" unless defined($pid); exit if $pid; close STDIN; open(STDOUT, '>>access.log'); open(STDERR, '>>error.log'); POSIX::setsid(); umask(0); print STDERR "daemon started as pid $$\n"; $SIG{INT} = sub { $die = 1; close STDERR; close STDOUT; }; $SIG{HUP} = sub { print STDOUT "HUP caught\n" }; until ($die) { sleep(2); }
My problem is (specifically) - the "HUP caught" never gets logged to the STDOUT log file.

If I replace all instances of STDOUT with STDERR - I will see the "HUP caught" message in its respective log file. STDERR works just fine.

If I replace all instances of STDOUT with some other descriptor (LOG) - I still do not see the HUP message.

Adding 'select(STDOUT)' does no good.

So, is STDERR the only available descriptor to a daemon process (surely not) - or am I missing something?
and if I am missing something, what is it?

Thanks,
Jeff

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)

Replies are listed 'Best First'.
Re: STDOUT and daemon processes
by Fastolfe (Vicar) on Jan 04, 2001 at 03:08 UTC
    Could this be a buffering thing? STDOUT by default is buffered while STDERR is not. Try turning on autoflush:
    $|=1;
      Doh! I knew it was something simple. Thanks!

      /me chants mantra over and over:
      'SDTERR is by default unbuffered, the others are not . . .'

      Jeff

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)