ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.