in reply to Reading from a UNIX daemon

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