When it comes to writing daemons, I prefer not to have to think about daemons at all. I write a program that runs "in foreground" (whatever that means), it does not fork to "run in background", it logs by simply writing to STDERR, and it does not maintain a PID file. That reduces the amount of code needed.

How do I get my programs to run as daemons "in background"? Easy, I use djb's daemontools, or one of the various toolsets using the same concept, e.g. runsv from busybox.

Daemontools handle all of the daemonizing, i.e. running "in background", switching user IDs, configuration, reliable logging even across program restarts and crashes, starting and stopping, sending signals to the daemon, and so on. See also Re^3: Proc::PID::File problem generating pid files, or: does it matter where a pid file lives?, Re^3: How to capture system return code when command launched in the background?, Re: Executing script in normal/daemon mode and adding start/stop capability, Re^5: A few questions regarding Proc::Daemon, daemontools at "the djb way".

And by the way: you simply do not need any PID file with daemontools. Of course, you can run multiple instances of the same program with different options via daemontools.

Note that you also do not need any daemonizing code in your program when you use daemontools, not even Proc::Daemon. In fact, such code makes it harder to use daemontools.

So, how hard is it to write a daemon using daemontools? Less than 10 lines of simple shell code, including configuration and logging.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: Running multiple instances of a daemon simultaneously? by afoken
in thread Running multiple instances of a daemon simultaneously? by Trenin

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.