calling a shell script in background and wait using "wait" in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calling a shell script in background and wait using "wait" in while loop
# 1  
Old 12-26-2011
calling a shell script in background and wait using "wait" in while loop

Hi,
I am facing a strange issue,
when i call a script from my while loop in background it doesnt go in background, despite the wait i put below the whil loop it goes forward even before the process put in background is completed.

Code:
cat abc.txt | while read -u4 line
do
   #if line contains # dont proces that line and continue to next line
   echo $line | grep '#' > /dev/null
   if [[ $? -eq 0 ]]; then
        echo ...skipping comment
        continue
   fi
   # extracting the values from cfg file
   username=`echo $line | cut -d" " -f1`
   hostname=`echo $line | cut -d" " -f2`
   dir=`echo $line | cut -d" " -f3`
   type=`echo $line | cut -d" " -f4`
   dmnname=`echo $line | cut -d" " -f5`
   mnserver=`echo $line | cut -d" " -f6`
   . ${TOOL_DIR}/formatReport.sh $hostname $dmnname $mnserver $curhour $type $reportdir $dir $curdate &
   echo "Process ID = $!"
done
wait
#further processing

file contents for abc.txt
Code:
abc  sun1  /prod/var/1  web1    web_43_1   mt_027_9043_1
def  sun2  /prod/var/2  web2    web_43_2   mt_027_9043_2
pqr  sun3  /prod/var/3  web3    web_43_3   mt_027_9043_3

---------- Post updated at 06:23 PM ---------- Previous update was at 06:14 PM ----------

Any help will be appreciated.. Guys any pointers to what can be issue?
# 2  
Old 12-26-2011
It is probably because the while loop is executed in a subshell (right side of the pipe). Try using:
Code:
while read -u4 line
..
done < abc.txt
wait
#further processing

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-26-2011
If you use &, it will not wait for that process to complete i.e. it puts it in background and continue with its execution.
I am assuming that you want to wait till the processing is complete and then continue. If so, remove the & and try.

HTH
--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

When loading Linux "loading please wait.." then nothing?

Hi everyone, I have a problem but I have never installed a separate OS before so my lingo and understanding may not be as good as some of you. I will try and explain my problem best I can. I am trying to instead of loading Windows 7 when my computer starts up, for it to start linux specifically... (2 Replies)
Discussion started by: markhow30
2 Replies

2. Shell Programming and Scripting

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (14 Replies)
Discussion started by: thisissouvik
14 Replies

3. AIX

How to use 'expect' to pass UID & Password to a "for loop" in shell script?

Friends, Need someone's help in helping me with the below requirement for a script: > For a list of servers(over 100+), I need to login into each of them(cannot configure password-less ssh) & grab few configuration details < I know, this is possible through expect programming in a simple... (2 Replies)
Discussion started by: thisissouvik
2 Replies

4. Shell Programming and Scripting

How to increment date using "for loop" in format MMDDYY inside the shell script?

Need to increment the date from "currentdate + 90days" inside the for loop (i=1 to i=50) (5 Replies)
Discussion started by: aroragaurav.84
5 Replies

5. HP-UX

echo "selall;info;wait;infolog" | /usr/sbin/cstm problem

Hello, On a HP-UX 10.20 server I've executed something similar to this command: # echo 'selall;info;wait;infolog;view;done' | /usr/sbin/cstm But it returns sometype of "argument list too long" error. I suppose there is a way to fix it by using xargs but I can't figure it out. Any... (7 Replies)
Discussion started by: asanchez
7 Replies

6. UNIX for Dummies Questions & Answers

Do pipes know when they have to "wait" for all the data?

Hi, I was wondering if pipes ("|"), or rather the command that follow them, know when they're supposed to wait for all the data? For instance, if you take this: cat my_file | sort | uniq for uniq to work well, it needs to have rows sorted, but for lines to be sorted properly, it needs... (5 Replies)
Discussion started by: a.brassac
5 Replies

7. Shell Programming and Scripting

Don't wait for "read line"

Hi I am writing a bash script on Solaris, that should take n arguments, either appended to the script or taken as output from the last command (similar to grep). What I don't want is that the script waits for user input. In other words: Possibility 1: script.sh arg1 arg2 arg3 ...Possibility... (4 Replies)
Discussion started by: g000ze
4 Replies

8. Shell Programming and Scripting

wait in background

can a wait command be run in background? or the script which has the wait command, be run background? test.sh ------- nohup a.sh & nohup b.sh & wait nohup test.sh & How can i run either wait or test.sh in background? i want test.sh to wait till a.sh and b.sh complete, and must be... (1 Reply)
Discussion started by: albertashish
1 Replies

9. Shell Programming and Scripting

wait command - cat it wait for not-chile process?

Did not use 'wait' yet. How I understand by now the wait works only for child processes, started background. Is there any other way to watch completion of any, not related process (at least, a process, owned by the same user?) I need to start a background process, witch will be waiting... (2 Replies)
Discussion started by: alex_5161
2 Replies

10. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies
Login or Register to Ask a Question