0

I use a bash script which makes an ssh connection to other machines and executes scripts (which are stored on those machine).

  1. SSH to master to execute script 1
  2. SSH to master to execute script 2
  3. SSH to master to execute script 3
  4. SSH to node1 to execute script 1 etc..

Now my question is: Will the execution of script 2 on the master start before the script 1 (which takes 30 seconds) is finished? Will they run at the same time?

fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
DenCowboy
  • 10,114
  • 24
  • 80
  • 168
  • 1
    Like any other command, `ssh` runs until it's done, unless you specifically run it in the background. – tripleee Apr 19 '16 at 06:46

3 Answers3

1

I'm just giving here a more detailed answer than @tripleee...

Will execute script2 after the completion of script1:

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.

Community
  • 1
  • 1
bufh
  • 2,647
  • 24
  • 32
1

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.

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 (i.e. All work is done when the script exits)

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.

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

edhurtig
  • 2,213
  • 1
  • 23
  • 26
0

It depends. Compare

sleep 10
date

sleep 10 &
date
Michael Vehrs
  • 3,089
  • 9
  • 10