【问题标题】:Program execution halts while loop bash循环bash时程序执行停止
【发布时间】:2019-05-20 00:18:30
【问题描述】:
#!/bin/bash
while read LINE
    do
[ ! -f /tmp/$(basename $0) ] && cp $0 /tmp/ && konsole -e $0 && exit
rm /tmp/$(basename $0) # open separate window for code to run
    $LINE << EOF # read line (ssh cmd)
    cd /st/task/ #commands during ssh
    ./start.sh #need something to let this run and go back to beginning
EOF
done < pdns.txt

因此 start.sh 命令旨在连续运行,但它会阻止 while 循环迭代

【问题讨论】:

  • ssh 可能正在从标准输入读取,在 read LINE 有机会再次运行之前消耗了 pdns.txt 的其余部分。
  • 感谢您的回复,但问题如前所述,start.sh 没有按设计完成,我仍然需要完成循环的所有迭代
  • 然后在后台运行start.sh。 (ssh 从标准输入读取可能是您尚未注意到的问题,除非您已经将其配置为从 /dev/null 读取。)
  • 还有其他方法吗?我已经读到 bash 中没有 goto 命令,所以有人可以告诉我另一种方式,因为我想查看 start.sh 的输出,但我也希望 while 循环完成
  • “完成”是指您希望多个ssh 命令并行运行(每个都在一个单独的konsole 窗口中)?

标签: bash loops ssh while-loop


【解决方案1】:

我会将这个循环简化如下:

  1. 创建一个名为“aws.config”的ssh 配置文件(或任何您想要的;具体名称并不重要),为每个主机定义适当的别名,指定您需要的任何连接参数。

    Host hostone
       User ubuntu
       Hostname blah.aws.com
       IdentityFile blah.pem
    
    Host hosttwo
       User someotheruser
       Hostname foo.aws.com
       IdentityFile bar.pem
    
    # etc
    
    Host *
        RequestTTY no
        RemoteCommand "cd /st/stack && ./start.sh"
    
  2. 输入文件只需要配置文件中定义的主机别名列表。

    $ cat pdns.txt
    hostone
    hosttwo
    # etc
    
  3. 现在循环可以很简单

    while IFS= read -r host; do
        konsole -e "ssh -F aws.config '$host'" &
        # If your version of ssh doesn't support RemoteCommand, use
        # konsole -e "ssh -F aws.config '$host' 'cd /st/stack && ./start.sh'"
    done < pdns.txt
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2018-09-07
    • 2013-03-12
    • 2012-09-14
    • 2016-07-02
    • 2015-08-06
    相关资源
    最近更新 更多