简体   繁体   中英

Multiple remote SSH through bash scripting

Hi all,

I am trying to run a server on one host, and have 3 clients located on 3 different hosts run at the same time (in the background).

I made a bash script to do that. The problem is that the script currently does not wait for the client to finish: it ssh on each host sequentially. I would like to have the 3 hosts run in parallel please.

Here is the code:

#!/bin/bash
i="0"
dir="~/bin/"

while [ $i -lt 3 ]
do
  let number=10+$i
  ssh sshost$number 'cd $dir && java Main &' 
  let i=$i+1
done

I am trying to ssh to 3 different hosts (number gets changed on each iteration), then go to the directory and type java Main. My only problem is that it won't do it in parallel, and I can't figure out how to fix it.

I would appreciate any help.

Thank you very much.

Make the ssh command to run in background:

ssh sshost$number 'cd $dir && java Main &' &

You can call wait outside the while loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM