I have two PERL SCRIPT that stress CPU and MEMORY. I want to run them in parallel on hosts so I am trying to copy them on all the hosts and run them in parallel. I am utilizing a module Net::OpenSSH::Parallel to copy (which is working) and run the commands on all the nodes. I am not able to run these two SCRIPTS in parallel. I have tried to take the SCRIPT to background but only one of them runs. Could you guide where am I going wrong?

CPU SCRIPT #!/usr/bin/perl use strict; use warnings; my @cpu_cores = qw(4); while (--$cpu_cores[0] and fork) {}; while () {}; MEMORY SCRIPT #!/usr/bin/perl use strict; use warnings; print "$$"; fork for 1 .. 4; for (1 .. 10000) { my $a = "xxxxx" x int rand 10_000_000; my $b = ~$a; my $c = reverse $b; MAIN SCRIPT use strict; use warnings; use Net::OpenSSH::Parallel; use Term::ANSIColor; use DBI; #-------------------------------------------------------- #FINDING HOSTS FROM CLOUDERA MANAGER DATABASE #-------------------------------------------------------- print color 'bold blue'; print " Finding hostname of the hosts configured\n\n"; my $ip_address = `ping r01mgt -w 1 | awk -F "(" '/PING/{print \$2}' | +awk -F")" '{print \$1}'`; chomp($ip_address); # connect my $dbh = DBI->connect("DBI:Pg:dbname=xxxx;host=$ip_address", "xxxx", +"xxxx", {'RaiseError' => 1}); # execute SELECT query my $sth = $dbh->prepare("SELECT name FROM hosts"); $sth->execute(); # iterate through resultset and create an array my @hosts; while(my $ref = $sth->fetchrow_hashref()) { my($nodename, $hadoopname) = $ref->{'name'} =~ m/(\w+).(\w+)/; push(@hosts, "$nodename"); } @hosts = sort (@hosts); print color 'reset'; #----------------------------------------------------------- #COPYING THE PERL SCRIPTS AND EXECUTING THEM #----------------------------------------------------------- my $pssh = Net::OpenSSH::Parallel->new(); $pssh->add_host($_) for @hosts; $pssh->push('*', scp_put => '/root/cpu.pl', '/root/'); $pssh->push('*', scp_put => '/root/memory.pl', '/root/'); $pssh->push('*', command => '/usr/bin/perl', '/root/cpu.pl', '/tmp/cpu +'); $pssh->push('*', command => '/usr/bin/perl', '/root/memory.pl', '/tmp/ +memory'); $pssh->run; USING THREADS TO RUN SCRIPTS IN PARALLEL #!/usr/bin/perl use strict; use warnings; use threads; sub cpu { my @cpu_cores = qw(4); print "CPU"; while (--$cpu_cores[0] and fork) {}; while () {}; } sub memory { print "MEMORY"; fork for 1 .. 4; for (1 .. 10000) { my $a = "xxxxx" x int rand 10_000_000; my $b = ~$a; my $c = reverse $b; } } my $firstThread = threads->create(\&cpu); my $secondThread = threads->create(\&memory); $_->join() foreach ( $firstThread, $secondThread );

In reply to Running PERL SCRIPTS in Parallel by rahulruns

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.