简体   繁体   English

通过bash脚本编写多个远程SSH

[英]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). 我试图在一台主机上运行一台服务器,并在3个不同的主机上同时运行3个客户端(在后台)。

I made a bash script to do that. 我做了一个bash脚本来做到这一点。 The problem is that the script currently does not wait for the client to finish: it ssh on each host sequentially. 问题在于该脚本当前不等待客户端完成:它依次在每个主机上ssh。 I would like to have the 3 hosts run in parallel please. 我想让3台主机并行运行。

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. 我正在尝试将SSH绑定到3个不同的主机(每次迭代都会更改数量),然后转到目录并键入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命令在后台运行:

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

You can call wait outside the while loop. 您可以在while循环外调用wait

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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