通过ssh远程bash脚本执行导致本地脚本停止

我有一个设置,行为方式我不能让我的头。 所以我有一个本地bash脚本generateTrafficOnHosts.sh ,它从文件读取IP地址。 然后在每个远程主机上调用一个bash脚本getTraffic.sh ,也就是每个调用的IP地址。 但是,在通过ssh执行第一个远程执行之后,它就完成了。 这是我的本地generateTrafficOnHosts.sh脚本:

 #!/bin/bash # Connects to a series of hosts and runs the get-traffic.sh script on them. # The hosts are taken from a file with a list of randomly generated IDs, # while their IDs are taken from a connectfile. # ... # some convenience functions here # ... ####################################### # runs the traffic generation script on # a host. # Globals: # # Arguments: # <IP of the remote host> # Returns: # None ####################################### function run_on_host() { local RESULTS RESULTS=$(ssh -i key.pem user@"$1" '/home/user/get-traffic.sh 2>&1 >/dev/null') } ####################################### # Runs the load on the hosts # Globals: # HOSTSFILE # Arguments: # None # Returns: # None ####################################### function run_load_on_hosts() { while read line do local ip=$(get_ip_for_key ${line}) echo "getting IP for key in line ${line}" if [[ $ip != "" ]]; then echo "running on host $line($ip)" run_on_host "${ip}" else echo "error: LINE'$line', IP'$ip'" fi done < "$HOSTSFILE" } function main() { check "$@" run_load_on_hosts } main "$@" 

远程脚本get-traffic.sh看起来像这样:

 #!/bin/bash PORT=47111 # Time limit in seconds TIME_LIMIT=10 # Blocksize of the dd command: how much data shall be downloaded per chunk? BS=1M while [[ "$SECONDS" -le "$TIME_LIMIT" ]]; do # $SECONDS is a shell variable nc "${HOST}" "${PORT}" | dd count=1 bs="${BS}" iflag=fullblock > /dev/null done 

现在奇怪的行为:该脚本只运行在主机文件中的第一个主机。 如果我换行ssh -i ... ,比如sleep 1; echo "running..." sleep 1; echo "running..." ,一切运行顺利。 如果我用例如ls /home/userreplace远程命令/home/user/get-traffic.sh – 一切都很好,情况也是如此。 但是,如果我用lsreplace远程命令,脚本也会在第一次迭代之后返回。

我一直在跟踪这个bug几个小时,而且几个小时前我已经没有想法了。 任何人,请吗?!? :|

看看ssh手册,更特别的-t选项。

如果这不起作用,请尝试-tt选项。 另外,我认为你的get-traffic.sh脚本应该在最后exit 0