Thanks for the prompt replies: This is my daemon code.
use strict; use POSIX qw(setsid); chdir '/' or die "Can't chdir to /:$!"; open STDIN, '/dev/null' or die "Can't read /dev/null:$!"; open STDOUT, '>>/dev/null' or die "Can't write to /dev/null:$!"; open STDERR, '>>/dev/null' or die "Can't write to /dev/null:$!"; defined(my $pid = fork) or die "Can't fork:$!"; setsid or die "Can't start a new session:$!"; umask 0; # this point on its a polling program. Didn't copy it entirely. while (1) { my (@lines, @fields, @bytes, $mysecond1, $mysecond2, @throughput, @ban +dwidth); @lines = `/sbin/ifconfig eth0` or die("$!\n"); @fields = split(/\s+/, $lines[6]); $fields[6] =~ /(([^:]*):?){2}/; $bytes[0] = $2; $fields[2] =~ /(([^:]*):?){2}/; $bytes[2] = $2; $mysecond1 = time; sleep 15; @lines = `/sbin/ifconfig eth0` or die("$!\n"); @fields = split(/\s+/, $lines[6]); $fields[6] =~ /(([^:]*):?){2}/; $bytes[1] = $2; $fields[2] =~ /(([^:]*):?){2}/; $bytes[3] = $2; $mysecond2 = time;
I did try to turn my daemon to a socket just now but I hit a snag. The server code used the accept(COMM_SOCKET,SOCKET) function in a continuous while loop. This function causes the server to wait for a connection and stops the script at this point.

But at the same time, I want the daemon to continue to do the polling routine. Note also that the routine has a sleep function inside. It works like this. The program polls the number of bytes at a certain time. Then, I sleep it for lets say 15 seconds. Then, I poll the number of bytes again. Then, I compute the difference. This is supposed to run continuously in the background. When I want to retrieve the data, my main script will ask for the data. So, the idea would be to send it over a socket connection with the daemon being the server and the main script the client.

But, how do I get the daemon to continuously listen for a connection and at the same time run the polling routine that has a sleep function? Do I alter the server code or the polling routine code?

Thanks.

Edit by dws to rescue the formatting


In reply to Re: Reading from a UNIX daemon by mint1981
in thread Reading from a UNIX daemon by mint1981

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.