LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 12-20-2004, 01:37 AM   #1
subu_s
Member
 
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

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

Rep: Reputation: 46
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
 
Old 12-20-2004, 05:57 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,280

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


Reply


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


All times are GMT -5. The time now is 02:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration