DenCowboy
DenCowboy

Reputation: 15126

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).

  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?

Upvotes: 0

Views: 183

Answers (3)

edhurtig
edhurtig

Reputation: 2371

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

Upvotes: 1

bufh
bufh

Reputation: 3420

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.

Upvotes: 1

Michael Vehrs
Michael Vehrs

Reputation: 3373

It depends. Compare

sleep 10
date

sleep 10 &
date

Upvotes: 0

Related Questions