Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-20-2004, 01:37 AM
|
#1
|
|
Member
Registered: Jun 2004
Posts: 36
Rep:
|
Selectively 'waiting' in a shell script
I have an application (shell script) wherein my parent process spawns off many child processes. These will exit once they have completed their task.
After spawning the processes, parent spawns another process . This spawned process will be in an infinite loop wherein it keeps asking for an user input.
At the same time, I need the parent process to do a 'wait' till all the child processes(except the 'user input process' have exited. The 'user input process wont exit as it will run infinitely' . So, once the spawned processes have exited, the parent should kill the 'user input process' and the parent should exit
Flow:-
=====
parent process starts
while :
do
spawn child processes # Step 1
done
(user input process) & # Step 2
wait for the spawned processes in Step 1
Now the processes spawned in Step 1 have died
Kill the 'user input process' (Spawned in Step 2)
Parent exits
=====================================
Now how do I make my parent wait for all its children, except for the user input process?
|
|
|
|
12-20-2004, 03:46 AM
|
#2
|
|
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802
Rep:
|
If you are starting background process you can use the $! variable to monitor the process. This is the bash variable for the last process put in background.
EXAMPLE:
Flow:-
=====
parent process starts
while :
do
child_process &
CHILD=`echo $!`
done
while [ -d /proc/$CHILD ]
do (user input process) & #Step 2
done
Now the processes spawned in Step 1 have died
Kill the 'user input process' (Spawned in Step 2)
Parent exits
|
|
|
|
12-20-2004, 05:57 AM
|
#3
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
er, it's called 'wait'
from man bash:
Quote:
wait [n]
Wait for the specified process and return its termina-
tion status. n may be a process ID or a job specifica-
tion; if a job spec is given, all processes in that
job's pipeline are waited for. If n is not given, all
currently active child processes are waited for, and
the return status is zero. If n specifies a non-
existent process or job, the return status is 127.
Otherwise, the return status is the exit status of the
last process or job waited for.
|
regards, billy
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:15 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|