Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-20-2004, 01:37 AM   #1
Registered: Jun 2004
Posts: 36

Rep: Reputation: 15
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

parent process starts
while :
spawn child processes # Step 1

(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?
Old 12-20-2004, 03:46 AM   #2
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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.


parent process starts
while :
child_process &
CHILD=`echo $!`

while [ -d /proc/$CHILD ]
do (user input process) & #Step 2

Now the processes spawned in Step 1 have died
Kill the 'user input process' (Spawned in Step 2)

Parent exits
Old 12-20-2004, 05:57 AM   #3
Senior Member
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,485

Rep: Reputation: 223Reputation: 223Reputation: 223
er, it's called 'wait'

from man bash:


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
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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
Hard Drive Problems: timeout waiting for DMA; error waiting for DMA mintee Linux - Hardware 10 09-21-2007 05:06 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
bash script suggestions for waiting for ssh -L iggymac Programming 4 03-22-2005 07:04 PM
waiting script Sn7 Linux - Software 2 09-20-2004 12:50 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration