What I tend to do when faced with a problem like what you describe (< 1 min intervals, eternal life-span), is combine what you did and cron. In order to avoid nasty effects of eventual memory leaks and flush things, I code an absolute maximum of iterations, to the tune of:
while (++ $tries < $any_number_you_wish) { # You code here } exit;
and then have cron running this daemon periodically. Also, be sure to take a look at Proc::Daemon for detaching from your controlling tty, which is always nice for a daemon to do. You might also want to look at Proc::Watchdog for keeping a flag telling wether your daemon is running or not.

With a bit of code, you can have your daemon update a file containing its pid while it is running. At the beginning of its execution, it may look for this file and attempt a dummy kill to see if it's already there, dying in this case. Otherwise, processing begins for a given number of iterations.

cron could then run your daemon periodically (say, each minute, which would on average cause a 30 seconds outage) to insure it stays up whatever happens. If you go this way, I would defer the detach until basic tests (files and dirs exist, etc) have been performed, so that cron could also be used as a report mechanism.


In reply to Re: Safe to run a constant loop? by fokat
in thread Safe to run a constant loop? by Anonymous Monk

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.