为什么bash循环部署脚本似乎只能工作一次? [英] Why does bash loop deploy script only seem to work once?

查看:363
本文介绍了为什么bash循环部署脚本似乎只能工作一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些简单的脚本,用于菊花链连接在一起,在一个文件中列出的一组服务器上运行一个特定的脚本,每行一个。

I have a few simple scripts that are intended to daisy chain together to run a specific script on a set of servers all listed out in a file, one per line.

单服务器部署脚本包含以下内容:

The single server deploy script contains the following:

  1 #!/bin/bash
  2 
  3 file=$1
  4 host=$2
  5 
  6 scp ${file} ${host}:/tmp/
  7 USER=`whoami`
  8 ssh -t -t $USER@${host} /tmp/${file}
  9 
 10 ssh "${host}" /bin/rm /tmp/${file}
 11 exit

它在脚本上工作正常我已经安装了yum安装tomcat和符号链接hadoop / hbase配置到共享类目录。

It works fine on a script I have that yum installs tomcat and symlinks hadoop/hbase configs to the shared class directory.

第二个主要文件是deploy-all.sh,其目的是解析主机列表并运行所有这些主机的部署脚本:

The second major file is deploy-all.sh which is intended to parse a list of hosts and run the deploy script to all of them:

  1 #!/bin/bash
  2 
  3 script=$1
  4 
  5 cat dumbo-hosts | while read fileline
  6 do
  7 echo ${fileline}
  8 ./deploy.sh ${script} ${fileline}
  9 
 10 sleep 10
 11 done

发生的是脚本运行一次,然后for循环坏了...我有如下输出:

What happens is that the script runs once, and then the for loop is broken... I got something like the following output:

$ ./deploy-all.sh setup-tomcat.sh 
line is hadoop01.myhost
setup-tomcat.sh                               100%  455     0.4KB/s   00:00    
tcgetattr: Inappropriate ioctl for device
hadoop02.myhost
hadoop03.myhost
hadoop04.myhost
<succesful output of hadoop01 task>
...
Connection to hadoop01.myhost closed.

如果我注释掉ssh命令,循环将通过所有4个主机成功运行,所以我认为它是涉及到一旦ssh发生,stdio被切断。另外tcgatattr的错误有些关系到我。

If I comment out the ssh commands the loop runs succesfully through all 4 hosts so I presume it's something involving stdio getting cut off once the ssh occurs. In addition the tcgatattr error concerns me somewhat.

我该如何解决?究竟是什么导致tcgetattr错误(我甚至不确定是否相关)?
如果我在这里遗漏了一些非常明显的东西,那么对于脚本来说并没有太大的帮助,任何帮助都将不胜感激。

How can I get around this? What exactly is causing the tcgetattr error(I'm not even sure if it's related)? Haven't really done much with shell scripts so sorry if I'm missing something really obvious here, any help would be appreciated.

推荐答案

当ssh作为子进程的一部分运行时,ssh重用stdin文件描述符是一个问题。

It's a problem with ssh reusing the stdin file descriptor when running as part of the subprocess.

解决方法是在调用时使用-n来自非终端上下文的ssh。

The workaround is to use '-n' when invoking ssh from a non-terminal context.

option=-n
tty -s 2>/dev/null && option=

scp ${file} ${host}:/tmp/
ssh $option -t ${host} /tmp/${file}
ssh $option ${host} rm /tmp/${file}

这篇关于为什么bash循环部署脚本似乎只能工作一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆