Asynchronous shell scripts question - newbie

Hi All,

I am fairly new to UNIX and very new to this forum too. This is my first post here. Here is my scenario -

I have 3 scripts script1, script2 and script3. I want to start script1 and script2 asynchronously. Upon completion of both scripts script1 and script2, i want to fire the last script script3

I have no information about the platform/application/hardware as this is an interview question. The answer I gave was - I will include these scripts in a file. call 1 and 2 from there and let them run in background. upon successful return I will fire 3. The interviewer was not satisfied with this answer and told me to come up with an answer on next Monday (I have my on-site interview).

Could you please help me with this? Thanks a lot in advance

PS: I did search the forum and found general information about asynchronous lists and stuff. Since i am a newbie in UNIX platform it was kind of too general.

  • STA

The command you may not know about is:

wait

.

You already have half the answer. See how wait can help you here.

1 Like

If this needs to be done using Unix shell scripts, then here is a possibility:

  • there are three scripts
  • scripts #1 and #2 each create a file upon completion, the file will signal OK or FAIL return code of the corresponding script
  • script #3 (master) runs both scripts #1 and #2 in the background and waits for the resulting files. I will have a timeout here just in case
  • when both files are OK files (meaning scripts #1 and #2 run OK) run the last procedure #3, otherwise say what went wrong
  • always cleanup / remove the files from scripts #1 and #2

Just a note: To run a script in the background in unix one has to end the command with '&'

1 Like

Here is a simple example which should explain things to you.

#!/bin/bash

async1()
{
   sleep 5
   echo "from async 1"
   sleep 5
   echo "async 1 finished"
}

async2()
{
   sleep 3
   echo "from async 2"
   sleep 4
   echo "async 2 finished"
}

echo "main before"
async1 &
apid1=$!
async2 &
apid2=$!

echo "main PID=$$ asynch1 PID=$apid1 asynch2 PID=$apid2" 
wait $apid1 $apid2
echo "main after"   
exit 0
1 Like

Thank you. Yea, i came across "wait" through my search. And read about it. Please don't misunderstand me. I am not arguing in any way. I am just trying to understand how this will work exactly so I can convince the interviewer. So, in wait(n) 'n' can be a PID. If i don't specify 'n' the master script will wait for all scripts to finish and return and then continue the execution of further code. So, if i go with this approach how will i know what the PID of "script1 &" and "script2 &" is ?
OR if I go with the "wait", without arguments option, does it mean the script3 will read as simple as this :

<script3's code>
..........
script1 &
script2 &
wait( )
..........
<continue script3's code>

?

---------- Post updated at 06:54 PM ---------- Previous update was at 06:50 PM ----------

Thank you. That would be a good approach.

---------- Post updated at 07:02 PM ---------- Previous update was at 06:54 PM ----------

Thanks fpmurphy. You are awesome!
I got to know so many things from your sample code. $$ gives calling process' PID and $! gives the called process' PID.
I am sure I could always replace those functions with script file names.
Hats off to you guys. unix.com rocks!