Perl - How to do multiple child processes in parallel?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Perl - How to do multiple child processes in parallel?
I'm looking for a way in Perl to be able to take a list of servers, ssh multiple commands to it and store the results. If I do this process serially, sometimes one server will hang the whole script and if it doesn't, it still takes hours to complete.
I'm thinking what I need to do is make a parent loop that calls out a separate process that passes the server name to the child sub process and then executes all the commands I have defined in its own process. If one server 'hangs', at least that won't stop the script from doing all the other servers in the list.
I'm guessing using the fork() command would serve me best, however, all the online descriptions I have found have been vague at best. Any suggested reading that gives a better description, or anyone have a better suggestion? I've done this type of thing plenty on Windows with pushing psexec out in a loop, just not Unix.
I'm looking for a way in Perl to be able to take a list of servers, ssh multiple commands to it and store the results. If I do this process serially, sometimes one server will hang the whole script and if it doesn't, it still takes hours to complete.
I'm thinking what I need to do is make a parent loop that calls out a separate process that passes the server name to the child sub process and then executes all the commands I have defined in its own process. If one server 'hangs', at least that won't stop the script from doing all the other servers in the list.
I'm guessing using the fork() command would serve me best, however, all the online descriptions I have found have been vague at best. Any suggested reading that gives a better description, or anyone have a better suggestion? I've done this type of thing plenty on Windows with pushing psexec out in a loop, just not Unix.
Devon
When launching a process with 'system' you can launch it in the background - that's what the final '&' is for.
Why should he launch with system(), if all he needs is a SSH connection? system() launches a shell, which is a waste. DevonB,, do you understand the semantics of the fork + exec process, in general? If no, then you can find quite a lot of literature online to explain it. Ask back here for help with the parts that aren't clear.
In Perl, the usage is almost identical to what you would do in C, only easier (like most things in Perl vs.C).
The basic premise is that you can create an arbitrary number of child processes (using fork()), and each of them can exec() another process, thereby turning the child into a different program. That's how all processes are launched in Unix/Linux.
To get you started:
Code:
# When we launch child processes, we don't care when they die; we
# just don't want their corpses littering the process table...
$SIG{CHLD} = 'IGNORE';
my $kidPid = fork();
if( $kidPid ){ # I'm the parent
print "Process launched....\n";
}
else{ # I'm the child: my child PID is zero; I don't have a child
exec "/usr/bin/ssh", "username@123.123.123.123";
}
You can put this in a loop, launching as many child processes as you need.
Why should he launch with system(), if all he needs is a SSH connection? system() launches a shell, which is a waste. ...
Well, it really depends how the whole problem is solved. I solve it using 'system', creating a wrapper script and log file per job, and monitoring the log file contents. The point is debuggability, especially on not very reliable networks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.