Here's my guess for what you're trying to do. (Leaving the rename part incomplete in honor of your leaving the connection part incomplete.)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11146781 use warnings; use Path::Tiny; use Time::HiRes qw( time ); use IO::Socket; my $port = 3143; # to my https://rosettacode.org/wiki/Hunt_the_Wumpus# +Perl my @ips = map "192.168.1.$_:$port", 10 .. 20; # FIXME for testing my $logfile = path( "/tmp/d.11146781,log" ); # FIXME filename my $timeoutin = 0.1; # FIXME short for testin +g $logfile->spew( localtime() . " begin\n" ); my $start = time; for my $ip ( @ips ) { if( my $pid = fork ) {} # parent elsif( defined $pid ) # child { my $status = IO::Socket::INET->new( # test for connection PeerAddr => $ip, Timeout => $timeoutin, ) ? 'ok' : $@ =~ s/.*: //r; $logfile->append( sprintf "%s %s in %.3fs\n", $ip, $status, time - + $start ); exit; } else { die "$! on fork" } # failure } 1 while wait >= 0; $logfile->append( localtime() . " end\n" ); print $logfile->slurp; # FIXME for testing # rename here...

Outputs:

Mon Sep 12 09:54:46 2022 begin 192.168.1.13:3143 ok in 0.003s 192.168.1.14:3143 timeout in 0.102s 192.168.1.12:3143 timeout in 0.102s 192.168.1.16:3143 timeout in 0.102s 192.168.1.19:3143 timeout in 0.102s 192.168.1.10:3143 timeout in 0.103s 192.168.1.15:3143 timeout in 0.103s 192.168.1.11:3143 timeout in 0.103s 192.168.1.20:3143 timeout in 0.103s 192.168.1.18:3143 timeout in 0.103s 192.168.1.17:3143 timeout in 0.104s Mon Sep 12 09:54:46 2022 end

Note: I am using Path::Tiny ->append because it does file locking. I consider the fact that it is also doing open and close as insignificant.


In reply to Re: Multiprocess - child process cannot be finished successfully by tybalt89
in thread Multiprocess - child process cannot be finished successfully by wonderG

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.