user2569618
user2569618

Reputation: 687

Execute simultaneous scripts on remote machines and wait until the process completes

The original idea was copy out a script to each IP address which would do a yum-install some RPMs and some configuration steps on each machine. Since the yum-install takes about 20 minutes, the hope was to do the install simultaneously on each machine then wait for all the spawned processes to finish before continuing.

#!/bin/bash

PEM=$1
IPS=$2

for IP in IPS; do
   scp    -i $PEM /tmp/A.sh ec2-user@IP:/tmp
   ssh    -i $PEM ec2-user@$IP chmod 777 /tmp/A.sh
done

for IP in IPS; do
   ssh -t -i $PEM ec2-user@$IP sudo /tmp/A.sh &
done

wait
echo "IPS have been configured."
exit 0

Executing a remote sudo execute command in background on three IP addresses yields three error messages. Obviously, there's flaw in my logic.

Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo
sudo: sorry, you must have a tty to run sudo

All machines are CentOS 6.5

Upvotes: 3

Views: 1120

Answers (2)

louigi600
louigi600

Reputation: 743

You need to tell ssh not to read from standard input

ssh -n -t root@host "sleep 100" &

Here's an example

drao@darkstar:/tmp$ cat a
date
ssh -n -t me@host1 "sleep 100" & 
ssh -n -t me@host2 "sleep 100" & 
wait
date
darkstar:/tmp$ . ./a
Mon May 16 15:32:16 CEST 2016
Pseudo-terminal will not be allocated because stdin is not a terminal.
Pseudo-terminal will not be allocated because stdin is not a terminal.

[1]-  Done                    ssh -n -t me@host1 "sleep 100"
[2]+  Done                    ssh -n -t me@host2 "sleep 100"
Mon May 16 15:33:57 CEST 2016
darkstar:/tmp

That waited in all 101 seconds. Obviously I've the ssh keys so I did not get prompted fro the password.

But looking at your output it looks like sudo on the remote machine is failing ... you might not even need -n.

Upvotes: 1

Josh Beauregard
Josh Beauregard

Reputation: 2749

just to push some devopsy doctrine on you.

Ansible does this amazingly well.

Upvotes: 0

Related Questions