gpt4 book ai didi

bash - 循环脚本只执行一次 - Bash

转载 作者:可可西里 更新时间:2023-11-01 16:22:55 28 4
gpt4 key购买 nike

我有以下 bash 脚本:

#!/bin/bash
cat /etc/hadoop/conf.my_cluster/slaves | \
while read CMD; do
ssh -o StrictHostKeyChecking=no ubuntu@$CMD "sudo service hadoop-0.20-mapreduce-tasktracker restart"
ssh -o StrictHostKeyChecking=no ubuntu@$CMD "sudo service hadoop-hdfs-datanode restart"
echo $CMD
done

/etc/hadoop/conf.my_cluster/slaves 有5台slave机器的IP。 datanode 无法与 jobtracker 通信,因此解决方案是重新启动它。输出是:

ubuntu@domU-12-31-39-07-D6-DE:~$ ./test.sh 
Warning: Permanently added '54.211.5.233' (ECDSA) to the list of known hosts.
* Stopping Hadoop tasktracker:
stopping tasktracker
* Starting Hadoop tasktracker:
starting tasktracker, logging to /var/log/hadoop-0.20-mapreduce/hadoop-hadoop-tasktracker-domU-12-31-39-06-8A-27.out
Warning: Permanently added '54.211.5.233' (ECDSA) to the list of known hosts.
* Stopping Hadoop datanode:
stopping datanode
* Starting Hadoop datanode:
starting datanode, logging to /var/log/hadoop-hdfs/hadoop-hdfs-datanode-domU-12-31-39-06-8A-27.out
54.211.5.233

但是,在它应该运行的 5 个 IP 地址中,只有第一个被执行。我该如何解决?

最佳答案

让我们问shellcheck :

$ shellcheck yourscript

In yourscript line 3:
while read CMD; do
^-- SC2095: ssh may swallow stdin, preventing this loop from working properly.

In yourscript line 4:
ssh -o StrictHostKeyChecking=no ubuntu@$CMD [...]
^-- SC2095: Add < /dev/null to prevent ssh from swallowing stdin.

好了。

关于bash - 循环脚本只执行一次 - Bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24468568/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com