Thank you all. From the advice I got here, I looked closer at fork(), and found it is a perfect fit for what I want. I did some test scripts, and found that I can fork() and wait for the return while using the children to all print their output to a common (opened before the fork) file with the first line the PID of the process. Then I can sort it to get the output from each and check for errors.

This approach has proven to be simple (complicated until I got a little more familiar) and very effective. The result is that I can run all of them at once and wait for the response.

In case anyone wanted to see, this is the test script. The file that it opens contains a list of servers to hit, and the script just echos the host name followed by a 30 second wait both executed on the remote host.

#!/usr/bin/perl use strict; use warnings; use Net::SSH qw(ssh); my %serverPid; my ($testkey, $testvalue); die("Could not open file list: $!") unless open(FHServerList, "<", "./servers"); die("Could not open log file: $!") unless open(FHlog, ">>", "./log"); while(<FHServerList>) { my $serverName = $_; chomp($serverName); my $pid = fork(); if($pid == '0'){ #Child process print FHlog (ssh($serverName, "hostname; sleep 30")); exit(0) } else { $serverPid{$serverName} = $pid; } } while(($testkey, $testvalue) = each %serverPid) { waitpid($testvalue, 0); print ("Done with $testkey\n"); } close(FHServerList); exit()

It is not super clean (it will implode on itself if the fork fails), but it is functional, and will get cleaned up as I work on the full 'real' script.

Thanks again for the help guys.


In reply to Re: Do I need threads? by TechFly
in thread Do I need threads? by TechFly

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.