Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-04-2007, 01:16 PM   #1
Registered: Aug 2004
Location: London
Distribution: Gentoo
Posts: 277

Rep: Reputation: 30
Simple script to wait for another script

Hi there,

I have the need to run three different bash scripts on three different computers (which is slightly different for each computer) via ssh and then I want to run a command ONLY after all of these scripts have finished.

I was wondering if there was a way in the bash script to tell it to wait until the other scripts have finished successfully before it executes the final line. I know this is usually done by starting the command from the next line, but if we are using ssh to login to a remote machine and then run a script, how would we know if it has completed? So the order of things I want to do are:

ssh into comp1
run startscript1
ssh into comp2
run startscript2
ssh into comp3
run startscript3

<< I want the scripts to all run simultaneously rather than in a specific order >>
wait until all three scripts are finished
run final command

I have setup RSA keys so that I can login without passwords to each machine.. I was wondering I could implement the waiting in the bash script....

Any ideas?

Last edited by Maverick1182; 11-04-2007 at 01:18 PM.
Old 11-04-2007, 02:06 PM   #2
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
No problem.

ssh comp1 "startscript1" &
ssh comp2 "startscript2" &
ssh comp3 "startscript3" &
Old 11-04-2007, 02:19 PM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A little add-on: it may be useful to know that you can catch the process ID of the last job running in background by means of $!. This is useful if you want to wait for a specific job only, as in
my_command &
my_other_command &
wait $pid
The process IDs can be retrieved by jobs -p, too.
Old 11-04-2007, 02:47 PM   #4
LQ Newbie
Registered: Nov 2007
Location: Tucson, AZ
Distribution: Kubuntu/Gutsy
Posts: 26

Rep: Reputation: 15
Perl and forking

This is not 100% tested but should work:

#!/usr/bin/perl -w
use strict;

my @cmdlist = ("ssh localhost 'sleep 5;echo task 1 done'",
               "ssh localhost 'sleep 7;echo task 2 done'",
               "ssh localhost 'sleep 9;echo task 3 done'",

my @childPids = ();

for(my $i=0; $i < @cmdlist; $i++)
   print "Spawing command: $cmdlist[$i]\n";

   my $pid = fork();

   if ($pid) # parent
      push(@childPids, $pid);
   elsif ($pid == 0) # child
      die "Unable to fork process: $!\n";

print "Wait for all tasks to finish\n";
foreach my $pid (@childPids)
   waitpid($pid, 0);

system("ssh localhost echo 'final command'");
You will, of course, need to update the commands in the array and at the end of the script.

Last edited by poet_imp; 11-04-2007 at 05:31 PM. Reason: Minor typo in reference to array element
Old 11-05-2007, 03:45 AM   #5
Registered: Aug 2004
Location: London
Distribution: Gentoo
Posts: 277

Original Poster
Rep: Reputation: 30
Wow, thanks for the detailed help everyone. So even somehting as simple as ssh localhost script & with a wait command at the end will wait for all of those processes to finish running before it runs the last command? Linux gets cooler and cooler by the minute! I have to find time to create the script I need but thanks for the templates. It's really helped!


fork, perl, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Need help with a simple script shell script WindowBreaker Linux - Software 2 12-15-2005 12:45 PM
How to make a script wait for input farmerjoe Linux - General 2 12-28-2004 05:18 AM
Make script wait for input farmerjoe Linux - General 4 12-28-2004 01:49 AM
bash-script won't wait for application to finish TLV Linux - Software 24 09-30-2004 11:18 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:38 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration