asynchronous - How to run multiple perl open commands asynchronously and display output in order -


i'm attempting run multiple ssh commands multiple servers asynchronously, i'd capture output commands , display them in order. throw in additional curveball, i'd pid3 run once pid2 complete, , pid4 run after first 3 commands complete. how best accomplished?

example:

// $pid1 , $pid2 should run asynchronously $pid1 = open(my $ssh1, "|ssh -t -t runuser\@$server{'app'} 'sudo chef-client'");  $pid2 = open(my $ssh2, "|ssh -t -t runuser\@$server{'auth'} 'sudo chef-client'");  // command should wait $pid2 complete. $pid3 = open(my $ssh3, "|ssh -t -t runuser\@$server{'auth'} \"sudo -- sh -c '$update_commands'\"");  // command should wait $pid1-3 complete before running. $pid4 = open(my $ssh4, "|ssh -t -t runuser\@$server{'varn'} \"sudo -- sh -c '$varn_commands'\""); 

my (somewhat crude) solution far. feel there may more elegant way handle in perl, may job done:

# silence non-error output commands on first 2 servers: $pid1 = open(my $ssh1, "|ssh -t -t runuser\@$server{'app'} 'sudo chef-client > /dev/null'");  $pid2 = open(my $ssh2, "|ssh -t -t runuser\@$server{'auth'} 'sudo chef-client > /dev/null'");  if ($pid1) {     print "connecting $server{'app'}: chef-client";     while ( <$ssh1> ) {         print $server{'app'};         print $_;     } } close $ssh1 or die $!;  if ($pid2) {     print "connecting $server{'auth'}: chef-client";     while ( <$ssh2> ) {         print $server{'auth'};         print $_;     } } close $ssh2 or die $!;  # run pid3 once pid2 closed $pid3 = open(my $ssh3, "|ssh -t -t runuser\@$server{'auth'} \"sudo -- sh -c '$update_command'\""); if ($pid3) {     print "connecting $server{'auth'}: $update_command";     while ( <$ssh3> ) {         print $_;     } } close $ssh3 or die $!;  # run pid4 after previous commands have completed. $pid4 = open(my $ssh4, "|ssh -t -t runuser\@$server{'varn'} \"sudo -- sh -c '$varn_command'\""); if ($pid4) {     print "connecting $server{'varn'}: $varn_command";     while ( <$ssh4> ) {         print $_;     } } close $ssh4 or die $!; 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -