3

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

user2569618
  • 517
  • 3
  • 16
  • 42
  • 1
    See https://unix.stackexchange.com/questions/122616/why-do-i-need-a-tty-to-run-sudo-if-i-can-sudo-without-a-password – Andreas Louv May 12 '16 at 17:31
  • Thank you. However, the problem is not the execution of the sudo command, but the execution of a command in background on a remote system. Without the '&' the script works fine, but the command execution is sequential. I'm trying to fork a process so the execution happens simultaneously on several remote machines. – user2569618 May 12 '16 at 17:53
  • So let me get this right: you have N machines on which you want to run a command via ssh simultaneously and you want to wait for all of them to terminate ? – louigi600 May 16 '16 at 13:06

2 Answers2

1

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.

louigi600
  • 716
  • 6
  • 16
0

just to push some devopsy doctrine on you.

Ansible does this amazingly well.

Josh Beauregard
  • 2,498
  • 2
  • 20
  • 37
  • Yes, Ansible, Puppet, and Chef would do the job very well. But since I was in Bash, I was hoping to continue in Bash. I found "pssh" which creates parallel ssh jobs and waits for all the spawn jobs to return. – user2569618 May 19 '16 at 22:32