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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |