in reply to Backgrounding (daemonizing?) a Net::server
The following code almost works. See the note at the end to get it to really work.
#!perl -w use IO::Socket::INET; use Getopt::Std; use Proc::Daemon; use strict; my %opt; getopts("p:", \%opt); my $port = $opt{p} || 1923; my $listensock = IO::Socket::INET->new(LocalPort => $port, Listen => 5 +) or die "Socket: $!\n"; print STDERR "Process listening on port $port\n"; Proc::Daemon::Init(); # WARNING SEE NOTE while (my $client = $listensock->accept) { print $client "The time is ", scalar(localtime), "\r\n"; close($client); } # NOTE: This does not work because Proc::Daemon::Init closes ALL file # descriptors # If you delete this line from Daemon.pm # foreach $i (0 .. OpenMax) { POSIX::close($i); } # then it will work. Or you can just create your own function # which does everything that Init() does except that line
|
|---|