This is not 100% tested but should work:
Code:
#!/usr/bin/perl -w
use strict;
my @cmdlist = ("ssh localhost 'sleep 5;echo task 1 done'",
"ssh localhost 'sleep 7;echo task 2 done'",
"ssh localhost 'sleep 9;echo task 3 done'",
);
my @childPids = ();
for(my $i=0; $i < @cmdlist; $i++)
{
print "Spawing command: $cmdlist[$i]\n";
my $pid = fork();
if ($pid) # parent
{
push(@childPids, $pid);
}
elsif ($pid == 0) # child
{
system($cmdlist[$i]);
exit(0);
}
else
{
die "Unable to fork process: $!\n";
}
}
print "Wait for all tasks to finish\n";
foreach my $pid (@childPids)
{
waitpid($pid, 0);
}
system("ssh localhost echo 'final command'");
You will, of course, need to update the commands in the array and at the end of the script.