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)

In reply to STDOUT and daemon processes by jeffa

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.