Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How Can I Reliably Cleanup Sockets in a Backgrounded Program?

by xarex (Initiate)
on Jul 02, 2007 at 12:45 UTC ( #624429=perlquestion: print w/replies, xml ) Need Help??
xarex has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

i have the following......
#!/usr/bin/perl use warnings; use strict; #!/usr/bin/perl use warnings; use strict; use IO::Socket; $| = 1; my $port = 7777; my $server = IO::Socket ->new( Domain => PF_INET, LocalPort => $port, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1, ); die "Bind failed: $!\n" unless $server; #tell OS to clean up dead children $SIG{CHILD} = 'IGNORE'; print "Multiplex server running on port $port...\n"; while(my $connection = $server->accept){ my $name = $connection->peerhost; my $port = $connection->peerport; if (my $pid = fork){ close $connection; next; # on to the next connection }else{ # child process - handle connection print $connection "You're connected to the server!\n"; while (<$connection>){ use HTTP::Date; my ($date, $time) = split(" ", HTTP::Date::time2iso()); my ($hour, $min) = split(":", $time); my $log; open ($log, '+>>',"logs/$port $date.txt") || die "Couldn't op +en log.txt: $!"; print $log $_; close $log; my $dir = "logs"; my $file = "logs/$port $date.txt"; chmod (0777, $file); chmod (0777, $dir); } $connection->shutdown(SHUT_RDWR); } }
Basically all it does is listen for conections on a certain port number, and then writes the data recieved to a log file,
the above code works 100% for what i need,
My problem is if i start the script using Putty SSH like so ./
then its all fine, it creates the connections and if the connection is terninated it cleans up the dead children from the server, and releases the port again,
however, if i start the script like so
nohup ./
then it doesnt cleen up the dead children and keeps the port also in use after the host has disconnected
I need this file to be running all the time, could someone please tell me the correct way of doing this, or is there a problem in my script somewhere?

Please guys i really need to try and get this fixed


Replies are listed 'Best First'.
Re: How Can I Reliably Cleanup Sockets in a Backgrounded Program?
by jasonk (Parson) on Jul 02, 2007 at 15:43 UTC

    You are probably having issues because you set $SIG{CHILD} when you meant to set $SIG{CHLD}.

    We're not surrounded, we're in a target-rich environment!
Re: How Can I Reliably Cleanup Sockets in a Backgrounded Program?
by Moron (Curate) on Jul 02, 2007 at 13:20 UTC
    $SIG{CHILD} = 'IGNORE' is supposed to be supported for "some systems", but a more reliable solution is a specific reaper - ctrl-f for "fork" within perlipc for details.

    ^M Free your mind!

      Thanks for the reply,
      But i have absolouty no idea what you mean, could you please try to be a little more specific because i am a complete idiot


        I believe Moron meant that your $SIG{CHILD} = 'IGNORE' instruction should be avoiding zombification of your children, but it's not, and that you should read perlipc for more information on how to build a reliable reaper for your dead processes. Something like (from perlipc itself):

        use POSIX ":sys_wait_h"; sub REAPER { my $child; while (($child = waitpid(-1,WNOHANG)) > 0) { $Kid_Status{$child} = $?; } $SIG{CHLD} = \&REAPER; # still loathe sysV } $SIG{CHLD} = \&REAPER; # do something that forks...

        But you should check with him, anyways :-)

        I myself don't think the nohup command will behave as you might expect, and you should look for daemonization solutions for your program to keep working even if you disconnect. Look for "Complete Dissociation of Child from Parent" again in perlipc.

        Hope this helps!

Re: How Can I Reliably Cleanup Sockets in a Backgrounded Program?
by chrism01 (Friar) on Jul 03, 2007 at 00:42 UTC
    In addition to the above, it's a bit unusual not to background the process as well ie

    nohup ./ &


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624429]
Approved by Mutant
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2018-06-18 21:19 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.