简体   繁体   English

使用SSH启动bash脚本

[英]Start bash script with SSH

I use a bash script which makes an ssh connection to other machines and executes scripts (which are stored on those machine). 我使用bash脚本,该脚本与其他计算机建立ssh连接并执行脚本(存储在这些计算机上)。

  1. SSH to master to execute script 1 SSH掌握执行脚本1
  2. SSH to master to execute script 2 SSH掌握执行脚本2
  3. SSH to master to execute script 3 SSH掌握执行脚本3
  4. SSH to node1 to execute script 1 etc.. SSH到node1以执行脚本1等。

Now my question is: Will the execution of script 2 on the master start before the script 1 (which takes 30 seconds) is finished? 现在我的问题是:在脚本1(耗时30秒)完成之前,是否可以在主服务器上开始执行脚本2? Will they run at the same time? 他们会同时运行吗?

I'm just giving here a more detailed answer than @tripleee... 我只是在这里给出比@tripleee更详细的答案...

Will execute script2 after the completion of script1: 将在script1完成后执行script2:

ssh master script1
ssh master script2

You could do the same in one connection: 您可以在一个连接中执行相同的操作:

ssh master 'script1 ; script2'

If you want them to run at the same time: 如果希望它们同时运行:

ssh master 'script1 &; script2 &'

To cite the manual : 引用手册

If a command is terminated by the control operator & , the shell executes the command in the background in a subshell. 如果命令由控制操作符&终止,则外壳程序将在子外壳程序的后台执行该命令。

There are posts (like here and here ) on how to run multiples commands in one connection. 关于如何在一个连接中运行多个命令的文章(如此此处 )。

This would be dependent on how your bash script on your machine (which does the SSH connection to the servers) and the scripts on those servers works. 这将取决于计算机上的bash脚本(与服务器建立SSH连接)以及这些服务器上的脚本的工作方式。

For example: The following commands are completely procedural and not parallelized so long as scripts 1, 2, and 3 are procedural as well and do not fork work into the background before exiting (ie All work is done when the script exits) 例如:以下命令是完全过程性的,并且没有并行化,只要脚本1、2和3也具有过程性,并且在退出之前不会将工作分叉到后台(即,所有工作都在脚本退出时完成)

ssh master 'script1' 
ssh master 'script2' 
ssh master 'script3' 

This can be simplified as 这可以简化为

ssh master 'script1; script2; script3' 

You could parallelize the work by forking these scripts to the background, thus running scripts 1, 2 and 3 at the same time. 您可以通过将这些脚本派生到后台来并行化工作,从而同时运行脚本1、2和3。

ssh master 'script1 &; script2 &; script3 &' 

Again, just make sure that your scripts don't fork work into the background themselves and exit before the work is done 同样,只需确保脚本不会将工作本身分叉到后台并在完成工作之前退出

It depends. 这取决于。 Compare 相比

sleep 10
date

sleep 10 &
date

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

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