[Israel.pm] fork and rsh top

Offer Kaye offer.kaye at gmail.com
Thu Jul 15 05:53:43 PDT 2004


Hi all,
I've been running the following script to get the top 4 processes on a
list of some of the hosts on our network:

#!/bin/tcsh -f
set stations = (list of hosts)
foreach station ($stations)
   echo "##### $station #####"
   rsh $station "top n1 b | head -14 | tail -4"
   echo ""
end
# The End!

This actually worked fine, but a little slow, since each host is
accessed after the other, rather than in parallel.

Question 1 - can this be made parallel in tcsh? bash?

Anyway, I decided to try to do it faster in Perl by using "fork":

#!/usr/bin/perl
use strict;
use warnings;
use Proc::Daemon;
my @hosts = qw (list of hosts);
for my $host (@hosts) {
   my $pid;
   if ($pid = Proc::Daemon::Fork) {
   } elsif (defined $pid) {
      print "\n##### $host #####\n" . `rsh $host top n1 b | head -14 |
tail -4` . "\n";
      exit 0;
   } else {
      warn "Can't fork for host $host: $!\n";
   }
}
# The End!

This worked fine, as fast as I expected. However, here there were 2
small problems:
Question 2: The order of the reported stations was random. I guess I
should have expected this (due to the nature of fork), but is there a
way to get the outputs in the same order as the list of hosts?

Question 3: After the last output finishes I have to wait a few
seconds (exact time depends on the number of hosts) before the
terminal is "released" and I can type in it again. This BTW does not
happen if I replace the "top" command by for example 'uname -a'. Why
does this happen and how can I stop it from happening?

All the hosts are Linux hosts, I'm running perl 5.8.0.

Thanks in advance,
-- 
Offer Kaye



More information about the Perl mailing list