Asynchronous shell scripts question - newbie


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Asynchronous shell scripts question - newbie
# 1  
Old 12-22-2010
Question 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
# 2  
Old 12-22-2010
The command you may not know about is:
Code:
wait

.

You already have half the answer. See how wait can help you here.
This User Gave Thanks to purdym For This Post:
# 3  
Old 12-22-2010
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 '&'
This User Gave Thanks to migurus For This Post:
# 4  
Old 12-22-2010
Here is a simple example which should explain things to you.
Code:
#!/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

This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 12-22-2010
Quote:
Originally Posted by purdym
The command you may not know about is:
Code:
wait

.

You already have half the answer. See how wait can help you here.
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 ----------

Quote:
Originally Posted by migurus
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 '&'
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

General question about passing variables among shell scripts

So this is something I've been wondering how to do for a while. Suppose I have two shell scripts a.sh and b.sh script a does some function and outputs to a varable $x . I would then like to take $x into the second function, b.sh, and do a function on it and create some output. So how do you pass... (3 Replies)
Discussion started by: viored
3 Replies

2. Programming

need help with shell script filtering files and sort! newbie question?

Hi folks, I would like to get familiar with shell script programing. The first task is: write a shell script that: scans your home-folder + sub-directory for all txt-files that all users of your group are allowed to read and write then output these files sorted by date of last... (4 Replies)
Discussion started by: rollinator
4 Replies

3. Shell Programming and Scripting

Newbie Question: What is php shell scripting?

I know php is a Web Development language but what does it have to do with shell scripting. I might be wrong about php. Is there a CLI? How do I make one and how does it work? Please don't answer these if you have any books on this. Please give names of good beginner books for php shell scripting... (3 Replies)
Discussion started by: orszhak
3 Replies

4. Shell Programming and Scripting

Newbie Question: Killing a process using a supplied name to a shell script

Hi, I am trying to automate the killing of named processes of which I found a good solution here on the forums but as I am pretty much a begginer to linux I am having an issue. The code I found is: kill $(ps -ef | nawk '/monitoreo start/ { print $2}'} but what I want to do is replace... (3 Replies)
Discussion started by: TylrRssl1
3 Replies

5. Shell Programming and Scripting

Newbie question-do I pause and exec my shell scripts?

Hi there, I'm writing a script to restore 3 directories and 3 mysql db's every 24 hours. While writing the script, I came across the thought... Do I need to pause between executions? Example: msql restore1.sql rm -Rf path1/* cp -Rf path1/* /restore1 chmod 777 /folder1/cache1 ... (2 Replies)
Discussion started by: semiotics101
2 Replies

6. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

7. UNIX for Dummies Questions & Answers

shell scripting newbie question

Hi all! I'm a newbie to shell scripting. I want to create a script that will store a line from a text file in a variable so I can then use it to open firefox with that text in the address bar (the text file contains a list of addresses). I have tried the following: #!/bin/sh a='sed -n 2p... (2 Replies)
Discussion started by: jazzman
2 Replies

8. Shell Programming and Scripting

shell scripts question

Hi I am trying to do the following use a shell script to draw from various online table to the table on my server. it draws from one online table which has a set of columns one of which is in the form say 2006ab i want to use this as input to copy into another column an http link that has... (0 Replies)
Discussion started by: wannabegeek
0 Replies

9. Shell Programming and Scripting

two simple question on shell scripts....

:D my first question is how can envoke filename expansion from within a shell script.. forinstance if i execute the script and it asks for a specific directory that i would like to cd to, how can i get the script to use the same file name expension as the command line.. my second question is how... (3 Replies)
Discussion started by: moxxx68
3 Replies

10. UNIX for Dummies Questions & Answers

help for newbie writing shell scripts

Hi, I have just started a Systems Programming course and am required to write a c shell script as part of it. I have little to no clue about unix. The spec states that the script can be run only once on each host by a user, but the script can be run on different hosts by the one user. ... (2 Replies)
Discussion started by: richgi
2 Replies
Login or Register to Ask a Question