in reply to Kill a child nicely
YAATIUOWBWOL - Yet another attempt that is untested on windows but works on linux.
#!/usr/bin/perl use strict; # https://www.perlmonks.org/?node_id=11154445 use warnings; use IO::Socket; use IO::Select; my $port = 3333; # FIXME my $childstayup = '/tmp/childstayup'; # FIXME open my $fh, '>', $childstayup or die $!; close $fh; my $listen = IO::Socket::INET->new( LocalPort => $port, Listen => 100, ReusePort => 1,) or die $&; if( my $pid = fork ) # parent { print "Hit <ENTER> to quit\n"; <STDIN>; unlink $childstayup; 1 while wait > 0; print "parent exiting\n"; } elsif( defined $pid ) # child { my $sel = IO::Select->new( $listen ); while( -e $childstayup ) { for ( $sel->can_read( 1 ) ) { if( my $live_socket = $listen->accept ) { my $input = ''; while( <$live_socket> ) { $input .= $_; /^\r?\n?\z/ and shutdown($live_socket, 1), last; } print "process 'do stuff' here with:\n$input"; } } } print "child exiting\n"; exit; } else # bummer { die "fork failed $!"; }
Sort of a combination of mine and yours...
UPDATE: fixed 'for' variable name
|
---|