cybertextron
cybertextron

Reputation: 10971

loop script executing only once - Bash

I have the following bash script:

#!/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 has the IP of 5 slave machines. The datanode couldn't communicate to the jobtracker, so the solution is to re-start it. The output is:

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

However, out of the 5 ip addresses it was supposed to run, only the first is executed. How could I fix that?

Upvotes: 0

Views: 172

Answers (1)

that other guy
that other guy

Reputation: 123540

Let's ask 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.

And there you go.

Upvotes: 6

Related Questions