Hi,

I have written a daemon with the help of this post Re: Writing a Perl Daemon. The daemon more or less follows the structure shown there. The daemon is installed in CentOS 7 within systemd environment. It is declared there as forking type of daemon with a PID file and everything that goes with it.

Now I have a problem with it. The daemon sends data to two servers (for redundancy purposes) every two seconds. When both remote servers are up, everything is fine. However, when one of the servers is down, there are problems. The procedure waits for disconnected server to respond and at that time the data are not sent to the connected server too.

I need the daemon to have two child processes. Each one would send the data to one of the servers. In that case difficulties with one server would not cause problems with another one. I am not sure if Proc::Daemon and Proc::PID::File support more than one child process.

I tried something like this:

foreach my $server (@servers) { $i++; # # fork and background process # our $ME = $0; $ME =~ s|.*/||; our $PIDFILE = $PIDDIR . "/$ME.pid" . $i; my $pid = 0; $daemon{$server} = Proc::Daemon->new ( pid_file => $PIDFILE, work_dir => cwd () ); $pid = $daemon{$server}->Init (); if ($pid == 0) { # do the child stuff } }

When I run this daemon it creates children processes and more PID files. However, when I try to kill them, it is not so straightforward, because some a new child appears and I have to kill it too. I think I could resolve this issue, but there are more.

Since I use systemd where I have to specify PIDFile, I am also not sure if it is supported to have more PID files. I guess the right way would be to put all children PIDs into one PID file. Until now I was only testing this multiple children version with running the daemon manually. The strange behaviour with popping up children after killing the original ones stopped me from going further. I did not try to do it with systemd yet.

Where can I find some examples how to make a PERL daemon with more child processes? Since I use Proc::Daemon, Proc::PID::File, I would prefer to make it work with these libraries. I hope it can be done with them. Otherwise, I have to change quite a lot.


In reply to Multiple forks with Proc::Daemon by markoh

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.