2

I have a bash script that fetches running selenium nodes, grabs their ID, and SSHs into them to perform configuration tasks.

#!/bin/bash

# retrieve list of running vagrant machines, filter them to selenium nodes, and
# process provisioning for each
vagrant global-status --prune | grep "selenium_node" | while read -ra line ; do
    echo -e "\033[32mBEGININNG ${line[1]} PROVISIONING\033[37m"

    # adding this statement causes the loop to exit after 1 iteration
    vagrant ssh ${line[0]} -- "
        echo 'it runs'
    "

    echo -e "\033[32mEND ${line[1]} PROVISION\033[37m"
done

My problem is that running vagrant ssh causes the loop to terminate after the first iteration. I confirmed this by removing 'vagrant ssh' and the results were that both the BEGINNING and END echo commands ran successfully for every iteration (in my case - two iterations).

What's stranger is that the loop DOES complete it's first iteration (as evinced by the END echo line completing), it just doesn't run any further iterations.

Also, I've confirmed that it's not just neglecting to show the output from the other iterations. It never performs any operations on the other machines.

Silviu St
  • 1,810
  • 3
  • 33
  • 42

1 Answers1

1

ssh - including vagrant ssh - consumes standard input, so if you run it inside a while read loop, it won't leave anything for the next loop iteration to read.

You can fix that by either telling ssh not to read standard input (ssh -n) or by using a different construct than while read. In this case, since vagrant ssh doesn't support the -n option, I suggest running it with its input redirected from /dev/null:

</dev/null vagrant ssh ${line[0]} -- "
    echo 'it runs'
"
Mark Reed
  • 91,912
  • 16
  • 138
  • 175