【问题标题】:Execute simultaneous scripts on remote machines and wait until the process completes在远程机器上同时执行脚本并等待进程完成
【发布时间】:2016-09-08 16:28:30
【问题描述】:

最初的想法是复制一个脚本到每个 IP 地址,这将在每台机器上执行 yum-install 一些 RPM 和一些配置步骤。由于 yum-install 大约需要 20 分钟,因此希望在每台机器上同时进行安装,然后等待所有生成的进程完成后再继续。

#!/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

在后台对三个 IP 地址执行远程 sudo execute 命令会产生三个错误消息。显然,我的逻辑有缺陷。

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

所有机器都是 CentOS 6.5

【问题讨论】:

  • 谢谢。但是,问题不在于 sudo 命令的执行,而是在远程系统的后台执行命令。如果没有“&”,脚本可以正常工作,但命令执行是连续的。我正在尝试分叉一个进程,以便在多台远程机器上同时执行。
  • 所以让我来做对吧:你有 N 台机器,你想通过 ssh 同时运行一个命令,你想等待它们全部终止?

标签: bash ssh remote-access


【解决方案1】:

你需要告诉 ssh 不要从标准输入读取数据

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

这是一个例子

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

等待了 101 秒。显然我有 ssh 密钥,所以我没有收到密码提示。

但是看看你的输出,看起来远程机器上的 sudo 失败了......你甚至可能不需要 -n。

【讨论】:

    【解决方案2】:

    只是为了向你推销一些 Devopsy 教义。

    Ansible 在这方面做得非常好。

    【讨论】:

    • 是的,Ansible、Puppet 和 Chef 会很好地完成这项工作。但自从我在 Bash 中,我希望继续在 Bash 中。我发现“pssh”创建并行 ssh 作业并等待所有生成作业返回。
    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-26
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多