循环重击时程序执行停止

时间:2018-12-19 13:33:47

标签: bash loops ssh while-loop

#!/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循环迭代

1 个答案:

答案 0 :(得分:0)

我将简化此循环,如下所示:

  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