I needed a script that launched three instancies of the nmap network scanner to scan all our network in parallel. Each time a subnet is scanned and nmap finishes, another instance gets started to scan another subnet. This way I always have three parallel processes that do the job.

To learn how to do it I created this small script that could be a starting point for accomplishing similar tasks. You can substitute the sleep with an exec that runs an external program, for example, or fill that space with any perl you like!

Update: Thanks to other monks' suggestions I added "sanity checks": now the return value from fork and wait is checked. Thanks also to larsen, valdez, abell and alloalex's for their suggestions

#!/usr/bin/perl # forktest.pl # # I needed a script that launched three instancies of the nmap # network scanner to scan all our network in parallel. Each time # a subnet is scanned and nmap finishes, another instance gets # started to scan another subnet. This way I always have three # parallel processes that do the job. # # To learn how to do it I created this small script that could # be a starting point for accomplishing similar tasks. use strict ; use warnings ; my %childpid ; my $maxchild = 3 ; my $subnet = 1 ; do { while (keys(%childpid) < $maxchild and $subnet <= 254) { my $pid = fork ; # The following instruction comes from node 237098 die "Cannot fork: $!" unless defined $pid ; if ($pid) { # Parent process print "Child $pid started to scan subnet $subnet\n" ; $childpid{$pid} = $subnet++ ; } else { # Child process sleep rand 5 ; exit ; } } my $dead = wait ; # This die added after merlyn's suggestion # If you want to check a waitpid solution, see the code # posted by merlyn and zentara die "Something weird happened while wait!" if $dead == -1 ; print "Subnet $childpid{$dead} scan completed by child $dead\n" ; delete $childpid{$dead} ; } while (keys(%childpid) > 0) ;

In reply to Run N similar tasks in parallel by bronto

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.