Here's what my goal is:

I have an application that controls a printer. It can be given arguments to affect its behavior. It prints status messages to STDOUT. It is not written in Perl.

I would like my daemon script to spawn children, each one of which is an incarnation of this print app, run with its own arguments. I would like the output of each child to print to its own file in realtime (i.e. not buffered) so I can tail -f to see what a given printer is up to at any given time. The daemon should also listen for signals and terminate the children nicely.

Here's what I've got so far:

For testing purposes, replace the print app with this:

#!/usr/bin/perl -w use strict; while (1) { print localtime() . "\n"; sleep 1; }

Here is how far I've gotten with the daemon:

#!/usr/bin/perl -w use strict; use POSIX; use IO::File; my $time_to_die = 0; my $pid = fork; exit if $pid; die "Couldn't fork: $!" unless defined $pid; print "daemon pid = $$\n"; POSIX::setsid() or die "Can't start a new session: $!"; for my $n (1 .. 5) { my $pid; my $logfile = $n . '.log'; if ($pid = fork) { print "child pid = $pid\n"; } else { my $fh = IO::File->new; $fh->open("> $logfile") or warn "Couldn't open $logfile: $!\n" +; open (STDOUT, ">&" . fileno($fh)) or warn "Couldn't redirect S +TDOUT to $logfile: $!\n"; STDOUT->autoflush(1); $SIG{INT} = "IGNORE"; my $command = ("/home/brasey/daemon/timer"); exec $command; } } until ($time_to_die) { $SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&sig_handler; sleep; } exit 0; sub sig_handler { print "time to die\n"; $time_to_die = 1; kill ('HUP', -$$); }

(Also linked here)

Here's the skinny:

Basically, it all works, but output is buffered. I've tried these methods:

$| = 1; $^F = 10000; STDOUT->autoflush(1);

Where have I gone wrong?

Bob


In reply to Writing a perl daemon script - having trouble with output by brasey

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.