I'm trying to create a simple daemon process, ie, fork, exit the parent, and have the child inherited by the system to do its stuff.

I've tried this with the following code but running under cron doesn't seem to behave - I want it under cron to make make sure it restarts every hour in case it ever falls over.
my $pid = fork; if ( ! defined $pid ) { # fork failed throw_invalid "Couldn't fork"; } elsif ( $pid > 0 ) { # parent exit; } else { while ( 1 ) { sleep 1; # and do other snazzy stuff forever } }
$ ps -ef 

rbrown   29884  3162  0 11:58 ?        00:00:00 crond
rbrown   29885 29884  2 11:58 ?        00:00:03 solr-job-queue- <defunct>
rbrown   29886     1  2 11:58 ?        00:00:02 /usr/bin/perl -w /home/me/solr-job-queue-process.pl
Is it all because the parent can't die until it's child has, and that's why cron isn't clearing it from the process table?

Surely there's a way round this?

Any guidance greatly appreciated.

In reply to daemon/fork: defunct parent not going away nicely by Cagao

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.