adambot has asked for the wisdom of the Perl Monks concerning the following question:

when i execute the following code, when the script is done my console (bash) does not show any output. if i use zsh the issue does not happen, but unfortunately i'm not able to use zsh outside my development system. I've narrowed the issue down to something with the children. Here is the code that causes my issues:
#!/usr/bin/perl use strict; use warnings; use Nmap::Parser; my @pids; my $max = 10; my $children = 0; my $net = shift(); if (!defined $net) { $net = "192.168.1"; } foreach my $count (1..10) { my $ipaddress; $ipaddress = $net . "." . $count; my $pid; if($children == $max) { $pid = wait(); $children--; } if(defined($pid = fork())) { if($pid) { #parent $children++; push @pids, $pid; } else { #child my $ssh = my $rdp = " "; my $np = new Nmap::Parser; my $nmap_path = "/usr/bin/nmap"; my $nmap_args = "-Pn -p 22,3389"; $np->parsescan($nmap_path, $nmap_args, $ipaddress); my $host = $np->get_host($ipaddress); my @ports = $host->tcp_ports('open'); foreach my $port (@ports) { if ($port == 22 ) { $ssh = "Y"; } if ($port == 3389 ) { $rdp = "Y" } } printf "%-15s %-4s %-4s\n", $ipaddress, $rdp, $ssh; exit; } } else { print "Error: failed to fork\n"; exit; } } for my $pid(@pids) { waitpid $pid, 0; }
I'm hoping to not use any modules for children and just using built-in methods. Any help is appreciated.

Replies are listed 'Best First'.
Re: console output invisible after script exit
by LanX (Saint) on Jan 15, 2022 at 19:04 UTC
    > Any help is appreciated.

    Just a guess, maybe try setting autoflush $| in your children to avoid buffering?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      thanks for the reply! unfortunately $|=1 did not work. i set it first at the top of the script, that didn't work, so then i put it straight into the child code (right after #child).

      Oddly enough it is only when i actually run $np->parsescan($nmap_path, $nmap_args, $ipaddress);

      it doesn't look like it is an issue with the print statement because if i take it out the issue still happens. (i've been eliminating lines one by one which is how i got to the parsescan line being the problematic line but just in children)
        I think you should file a bug report and talk to the author.

        A quick look into the source showed that he is shelling out his calls, and this seems to collide with parallel processes. This might also explain the dependency on the shell...

        Just a guess ... 🤷🏻‍♂️

Re: console output invisible after script exit
by tybalt89 (Monsignor) on Jan 15, 2022 at 21:23 UTC

    Check the exit status of the children. It's in $? after a waitpid call.

      thanks for the reply! i just checked and all the children report an exit status of 0
Re: console output invisible after script exit
by etj (Priest) on Feb 27, 2022 at 20:14 UTC