I have a shell script that initiates a long, resource-intensive command on several different machines.

In order to execute the script on each machine in parallel, I have an ampersand symbol after each remote command in the for-loop.


while read host; 
    ssh -f "$host" \"/home/user/allocate.sh\" &
done < ~/cluster

However, as a consequence, I do not get the shell prompt back at the end of the for-loop. This prevents me from executing other scripts after initialize-cluster.sh has finished running allocate.sh on all of the machines.

For example, I want to be able to run a command such as:

./initalize-cluster.sh && ./execute-something-else.sh

However, currently initialize-cluster never "finishes."

I'd like subsequent commands to execute only after all of the allocate.sh scripts finish on the remote machines.

  • There is a #!/bin/sh. My intention was to provide the meaty part of the code.
    – eitan27
    Oct 17, 2014 at 19:36

1 Answer 1


Nothing in this script should prevent execution of the next command or return to the shell prompt. What could be giving the impression that the prompt is gone is the output of the remote scripts, which would arrive after return to the shell. To avoid that you could redirect stdout/stderr to some log file.

Since the remote commands are run in the background the original script will finish before the allocate scripts have finished executing: the next command can't assume that initialization is complete. In order to start all initializations in parallel and wait for them all to finish you could do the following:

while read host 
  ssh "$host" "/home/user/allocate.sh" &
done < ~/cluster

And then it should be possible to:

./initialize-cluster.sh && ./execute-something-else.sh

The key here is the wait command, which waits for child processes to finish.

  • There seems to be an error... Syntax error: word unexpected (expecting ")")
    – eitan27
    Oct 18, 2014 at 10:25
  • which shell? works for me in bash
    – artm
    Oct 18, 2014 at 13:41
  • you're right, if I change shebang to #!/bin/sh (which is dash on my system) I get the same error.
    – artm
    Oct 18, 2014 at 13:46
  • simplified it to work in more shells. I was overthinking it.
    – artm
    Oct 18, 2014 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.