Sachin
Sachin

Reputation: 375

How can I issue parallel commands to remote nodes with different arguments?

I need to execute an application in parallel on multiple Ubuntu-Linux servers while supplying different arguments for different servers. I tried to google it, but could not get to the possible solution. I even experimented with ssh/pdsh/parallel, but without success.

To explain the scenario further, here is a non-working example (with pdsh) where script.sh should be executed on all 3 servers in parallel but with different arguments. FYI, I already have public/private ssh-key (password-free login) in place.

$ pdsh -w server1,server2,server3 -l username script.sh args
where args should be 1 for server1, 2 for server2 etc.

I would appreciate if someone can help me achieve this, either using pdsh or some other tool available in Ubuntu. Thanks for your help.

Regards
Sachin

Upvotes: 3

Views: 1466

Answers (2)

Sameer
Sameer

Reputation: 2505

You don't need any special tools - ssh and bash are sufficient. If you want to add more servers, all you need to do is add them to the arrays near the top. Be careful with the spaces in the sample code below. Use multiple argument arrays if you have multiple arguments. An ssh config file allows per host user names.

`#!/bin/bash
servers=( server1 server2 server3 )
args=( args1 args2 args3 )
count=${#servers[@]}
k=0
while ((k<count))
do
  ssh -l username ${servers[k]} 'script ${args[k]}' &
  ((k++))
done`

Upvotes: 0

carl.anderson
carl.anderson

Reputation: 1118

I've done similar things using cssh in the past, but I don't see why pdsh wouldn't work with the same approach (although I've never used it).

My assumption is that you have an interactive session running simultaneously on all systems and you are able to create new environment variables in each session. If pdsh doesn't allow this, cssh does.

Before starting your script, set an environment variable based on the hostname.

ARG=$( case $(hostname) in server1) echo 1;; server2) echo 2;; server3) echo 3;; esac )
script.sh $ARG

Assuming the number you want is encoded in your hostname (as suggested in your question), you can simplify it like this:

script.sh ${HOSTNAME#server}

Upvotes: 1

Related Questions