I am using the following method to fork off and dissociate from the terminal for a script I want to daemonize. Now, I'm told I could just do some while(1) { sleep; } trickery, but I would think there'd be a way to do it without resorting to that. The script should last forever as is, as it uses File::Tail to watch a log. Permissions are OK, as if I remove the sighandler stuff, I can watch it output exactly what I'm looking for.
defined(my $pid = fork) or die "Can't fork: $!\n"; exit if $pid; setsid or die "Can't start a new session: $!\n"; + + $SIG{'HUP'} = $SIG{'INT'} = $SIG{'KILL'} = $SIG{'TERM'} = &sig_handler +; my $connection_log = "/var/log/qmail/qmail-smtpd/current"; if(-e $connection_log){ my $file = File::Tail->new($connection_log); my $line; while (defined($line = $file->read)) { print "Line contents: $line\n"; } } sub sig_handler { my $sig = shift; print "Received SIG $sig\n"; quit(0); }
Now, I would think that it would run forever (as long as the file was moving) and occasionally print the signals it received, except it dies immediately because the fork kills it. Does someone have a tutorial or a link they could present to do what I think I'm trying to do? Alternatively, education as to WIDIAW (why i'm doing it all wrong) would also be appreciated :) Does the file being rotated out kill File::Tail? The documentation suggests that it handles that situation. Update: It's fairly obvious why it's quitting to me (quit) Forget that part, the question still stands, am I daemonizing the wrong way?

In reply to fork process + SIG handlers by cidaris

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.