Home > IT Architecture, Perl, Programming > parallel execution of script using perl fork()

parallel execution of script using perl fork()

July 3rd, 2013

Let's assume that we want to test 100 hosts to see whether they're pingable or not. You may think of bash style for ... do ... done loop, but as that is executed one by one, so it's not efficient. And in this article, I'm going to create parallel execution of this task using perl's fork().

Firstly, here's one script to get whether one host is pingable or not:

[root@centos-doxer pping_doxer]# cat script.sh
log_user 0
match_max -d 2000000
set host [lindex [lrange $argv 0 0] 0]
spawn -noecho ping $host
set timeout 2
expect "bytes from" {exit 0} default {puts "not pingable from $host";exit 1}

Let's run it and see the result:

[root@centos-doxer pping_doxer]# ./script.sh test-host
not pingable from test-host

So now, let's write one perl script to wrapper this expect script, and also add parallel run to it:

our @hosts=("testhost0001","testhost00022","testhost00033","testhost0007");
#you can read a file to perl array too
#our @hosts=<$fh>;
our $zombies = 0;
our $kid_proc_num = 0;

$SIG{CHLD} = sub { $zombies++ };
for(my $i=0; $i<@hosts; $i++) {
my $pid = fork();
if( !defined($pid) ) { exit 1; }
unless($pid) {
system("/doxer/projects/pping_doxer/script.sh $hosts[$i]"); #change the path according to your env
exit 0;

while (1) {
if($zombies > 0) {
$zombies = 0;
my $collect;
while(($collect = waitpid(-1, WNOHANG)) > 0) {
if($kid_proc_num==0) { last; }
else { next; }

And now let's have a test:

[root@centos-doxer pping_doxer]# ./pping_doxer
not pingable from testhost0001
not pingable from testhost00033
not pingable from testhost0007
not pingable from testhost00022

You can modify script.sh to fulfill your own aim except for ping of course.

That's it, enjoy!


1.I've wrote an updated version of pping_doxer, which allows you to specify a maximum number of parallel running processes and also puts hosts in a file. I've put the package in http://www.doxer.org/projects-doxer/, the package name is pping_doxer.

2.Thanks very much to http://www.ibm.com/developerworks/cn/linux/l-cn-perlmp/.

Good Luck!

Categories: IT Architecture, Perl, Programming Tags: ,
  1. No comments yet.
Comments are closed.