1

I have a script that looks like this:

SERVER="RHT372 RHTl373 RHT374 RHTl375"

case $1 in
"stop")
  for SERVER in $SERVER
  do
    echo "================================================"
    echo Stopping $SERVER
    ssh  root@$SERVER /var/tmp/stopServer.sh server1 
  done
;;

How can I make the ssh wait for the command to finish then go to the next server and issue the same command?

user192506
  • 23
  • 1
  • 1
  • 4
  • 2
    This is not an approach that is scaleable or sustainable. If you're in an enterprise environment you might want to look at e.g. puppet, or other software specifically designed to handle this kind of job. – Jenny D Oct 03 '13 at 12:46

2 Answers2

1

To give you an example see this:

    #!/bin/bash

  (( $# != 1 )) && printf "%s\n" "Please provide one parameter"; exit 1;

    servers=( srv1 srv2 srv3 )

    case $1 in
    "stop")
        for srv in "${servers[@]}"
            do
     hostAlive=$(ping -s 64 "$srv" -c 1 | grep packet | awk '{print $(NF-4)}')

                echo "=========="
                echo "Stopping $srv"
        [[ "$hostAlive" == "0%" ]] && ssh root@$srv '/var/tmp/stopServer.sh server1'
            done;;
    "start")
        for srv in "${servers[@]}"
            do
                echo "=========="
                echo "Starting $srv"
                ssh....
            done;;
    *)
        echo "Not a valid option"
        exit 1;;
    esac

The ssh command will run only if the previous one has finished its operation. If you need to know more, see wait command.

Valentin Bajrami
  • 4,045
  • 1
  • 18
  • 26
  • There is a distinct lack of error handling in this script. – Jenny D Oct 03 '13 at 14:30
  • Added some checking as noted. – Valentin Bajrami Oct 03 '13 at 14:48
  • There's still no way to validate that the server was actually shut down/started, or to revert and try again if the script failed, or any logging of which servers were successfully started/shut down... which is why doing this kind of thing in a shell script is not a good long-term solution, to put it mildly. It's **possible** to do it, and you're off to a decent start, but it's not possible to do it in this way **and** keep it stable, resilient and scaleable. It's just the wrong scope for the solution. – Jenny D Oct 03 '13 at 15:10
  • Your script always executes the first `exit 1` and never does anything else. You need to change the semicolon to `&&` or place the `printf` and `exit` inside curly braces. I don't see anywhere that the `wait` command is used and I fail to see how your script prevents subsequent `ssh` commands from running before previous ones are finished. Also, how can a script start a server that's down? – Dennis Williamson Oct 04 '13 at 00:16
  • @DennisWilliamson The exit 1 should indeed have not been there. `if (( $# != 1 )); then printf "%s\n" "Usage $0 stop"; fi` is a more applying method. The `start` there is to show how to use the `case` function. The OP did not mention anything about `start` I added it to illustrate the usage of `case`. Then `wait` in scripts is used when you run things in the background in parallel. In this case it wouldn't matter weather `wait` is used or not. "" wait: Stops the execution of the current shell until active jobs have finished. "" – Valentin Bajrami Oct 04 '13 at 08:47
  • But the script should exit when the number of supplied arguments is wrong. Otherwise, the message is pointless. I know how `wait` works. The OP's question is about making his script wait until each `ssh` is finished before performing the next. You only mention `wait` in passing, but you suggest "If you need to know more..." and now you say "...it wouldn't matter...". There's nothing in your script that causes the requested wait (using `wait` or otherwise). You have illustrated a `case` statement, however. – Dennis Williamson Oct 04 '13 at 12:52
0

How long does it take until one server is shut down?

You can use sleep between the server-shutdowns.

Usage:

echo "Hi, I'm sleeping for 5 seconds..."
sleep 5
echo "all Done."