开发者

Multiple remote SSH through bash scripting

开发者 https://www.devze.com 2023-04-04 21:36 出处:网络
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).

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消