How do you wait for command substitution processes to complete?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do you wait for command substitution processes to complete?
# 1  
Old 02-22-2010
How do you wait for command substitution processes to complete?

When running a command using the >(cmd) syntax in bash how do you wait for the command to complete before moving on in your script?

Here is a simple example:
Code:
zcat largefile.gz | tee >(wc && echo “HELLO”) > /dev/null
# I tried wait, here but it doesn't wait for the process in the subshell.
echo "WORLD"

This produces the output:
Code:
WORLD
22245  175374 3598423
HELLO

My objective is to guarantee echo "WORLD" gets printed after all of the >() processes are done.

The bigger picture is that I have a lot of large log files to process through a variety of log parsers and using this syntax does have the positive effect of reading the files from disk once to process them in parallel in many sub processes taking advantage of multi-core CPUs. Everything works fine except for the fact that I can't tell when those processes are done.

I can think of one solution where I could have my log processing scripts write an "I'm Done" message to a file or pipe somewhere for the parent to loop listening for.

Is there a better way?
# 2  
Old 02-23-2010
I hope We can make use of wait and sleep command here .
# 3  
Old 02-23-2010
wait & sleep

wait doesn't work, I tried that. sleep might work most of the time, but it isn't a guarantee that the work would be done after sleeping.

I wrote up the following to do what I want, it works but I guess I was hoping for a more elegant solution like a simple flag for wait or something.

Code:
#!/bin/bash

DONE_PIPE=/tmp/donepipe$$
mkfifo $DONE_PIPE
exec 7<>$DONE_PIPE

zcat /logs/billing.log.2009/09/01/billing.log.20090901_101924.96.17.157.45.gz | tee >(wc && echo "HELLO" && echo "DONE" >&7) > /dev/null
COUNTER=0
#wait for the processors to complete
while [ $COUNTER -lt 1 ]; do
    read LINE <&7
    echo $LINE
    let COUNTER=COUNTER+1
done

exec 7>&-
exec 7<&-

rm $DONE_PIPE

echo "WORLD"

Which produces the following output:
Code:
22245  175374 3598423
HELLO
DONE
WORLD

With the above code multiple >(cmd) arguments can be given to tee and each would echo a statement to the 7 filedescriptor when they're complete. The while loop condition would just be adjusted to wait for however many processes you have launched using >(cmd).
# 4  
Old 02-23-2010
Hi.

You seem to be effectively doing:
Code:
zcat $FILE | wc ; echo "HELLO"

I suggest eliminating the tee, since you are throwing away the output from the zcat anyway ( >/dev/null ).

The FIFO that the shell creates for ">(" may be causing the problem. I did verify that the behaviour you posted is accurate -- at least it is with bash and ksh (and it surprised me). It seemed to work correctly with zsh, so you may wish to try that variant.

I didn't quite understand the bigger picture you described, so I cannot comment farther.

Best wishes ... cheers, drl
# 5  
Old 02-23-2010
Yeah, the example I gave was perhaps oversimplified and doesn't show the reason behind the complexity. My actual tee statement has 6 such >(cmd) commands given to tee and the amount of data being read from disk is hundreds of gigs of compressed logs. The reason I'm doing it this way is so that the data is read of disk once, decompressed once and sent to 6 different processing pipelines in parallel for analysis.

The reading from disk is what takes all the time on my machine so this saves having to do it six times. Plus it leverages the 4 core processor I have to do the log processing in parallel. Even with 6 things running in parallel the CPU is not maxed out, it's still the disk IO that dictates the pace of processing.

Without this method it was taking much longer to process these logs.

I don't have access to the source code for all the tools in each of the 6 analysis pipelines otherwise I suppose I could rewrite those tools to do the parallel processing internally.
# 6  
Old 02-23-2010
Like "drl" I don't get exactly what you are tying to achieve. I can't see why there is the complexity of redirecting channels.

It seems that you want to issue multiple command sets concurrently and wait until they have all completed.

IMHO forget using exec. It will drive you nuts with multiple concurrent processes.

Best approach is to background the command (or lots of commands).
[CODE]nohup command_name &[/CODE

Then check for background jobs.
Code:
jobs

When there are no background jobs running, all the background jobs for your user have completed.

I'll leave you to script processing the output (or not!) from the shell command "jobs". See "man" for your shell to read about "jobs".
# 7  
Old 02-23-2010
I'm trying to achieve the following goals:
1. Only read the log files from disk once.
2. Only decompress the log files once.
3. Process the log files with multiple parallel processes.
4. wait for them all to complete
5. do something else.

The reason being is that disk io and decompression are expensive in comparison to log processing, so I only want to do them once. And running the processing in parallel leverages my quad core processor to further reduce run time.

I think I have what I need, but I'm open to any other suggestions for how to do this better.

With background jobs method you suggest I would have to :
1. create an array of 6 unique temporary pipe names
2. mkfifo for each of the 6 pipes
3. start sending the decompressed logs to 6 named pipes using tee
4. launch a the 6 processing pipelines in the background telling them to read from the named pipes.
5. Now I could use wait to wait for them all.
6. rm all the temporary pipes
7. continue my script

This would work too, but is this better than using 'tee >(cmd1) >(cmd2)...>(cmd6)' and waiting for them to echo something into one named pipe that I have to manually manage? At least 'tee' takes care of creating and destroying the named pipes for me.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script parallel tasks and command to wait untill complete?

Hello, im having bash script with while *** command1 && command2 && command3 && done i want to ask how i can prevent overloading server, by waiting untill all commands complete? any low resources intensive command like "wait" - i dont know if exist? (2 Replies)
Discussion started by: postcd
2 Replies

2. Shell Programming and Scripting

In Shell Script Does Second Command Wait For First Command To Complete

Hi All, I have a question related to Shell scripting. In my shell script, I have following two commands in sequence: sed 's/^/grep "^120" /g' $ORIGCHARGEDAMTLIST|sed "s;$;| cut -f$FIELD_NO1 -d '|' | awk '{ sum+=\$1} END {printf (\"%0.2f\\\n\", sum/100)}' >$TEMPFILE mv $TEMPFILE $ORIGFILE... (3 Replies)
Discussion started by: angshuman
3 Replies

3. Shell Programming and Scripting

Wait for one processes to complete in a shell script

Let's say I start process A.sh, then start process B.sh. I call both of them in my C.sh How can I make sure that B starts its execution only after A.sh finishes. I have to do this in loop.Execution time of A.sh may vary everytime. It is a parameterized script. (17 Replies)
Discussion started by: rafa_fed2
17 Replies

4. Shell Programming and Scripting

Wait command help

Hi, Is there any way to know the child process status as and when it finished. If i write like below nohup sh a1.sh & ### has sleep 20 ;echo a1.sh nohup sh a2.sh & ### has sleep 10 ;echo a2.sh nohup sh a3.sh & ### has sleep 5 ;echo a3.sh wait This will wait till a1.sh ,a2.sh a3.sh... (0 Replies)
Discussion started by: patrickk
0 Replies

5. Filesystems, Disks and Memory

hdparm + HDIO_DRIVE_CMD(null) (wait for flush complete) failed: Inappropriate ioctl

Hi All, Am finding performance of my SD card using hdparm. hdparm -tT /dev/BlockDev0 /dev/BlockDev0: Timing cached reads: 1118 MB in 2.00 seconds = 558.61 MB/sec HDIO_DRIVE_CMD(null) (wait for flush complete) failed: Inappropriate ioctl for device Timing buffered disk reads: 14... (0 Replies)
Discussion started by: amio
0 Replies

6. 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

7. Programming

Howto spawn multiple child processes and wait?

As far as I can tell, the bash wait command waits for a logical "AND" of all the child processes. Assuming I am coding in C: (1) What is the function I would use to create multiple bash child process running perl? (2) What is the function I would use to reinvent the bash wait command so I... (4 Replies)
Discussion started by: siegfried
4 Replies

8. Shell Programming and Scripting

Wait Command

Does anyone have an example of a korn shell scripts kicking of multiple background processes and then using the wait command to get the return code from those processes? I want to write a program that kicks off multiple Oracle procedures and then wait for the return code before I procede.... (1 Reply)
Discussion started by: lesstjm
1 Replies

9. Shell Programming and Scripting

Wait for Background Process to complete

I am attempting within a for-loop, to have my shell script (Solaris v8 ksh) wait until a copy file command to complete before continueing. The specific code is: for files in $(<inputfile.lst) do mv directory/$files directory/$files ksh -m -i bg %% wait $! done I am shaky on the... (3 Replies)
Discussion started by: gozer13
3 Replies

10. Shell Programming and Scripting

PERL: wait for process to complete

I'm using PERL on windows NT to try to run an extract of data. I have multiple zip files in multiple locations. I am extracting "*.t" from zip files and subsequently adding that file to one zip file so when the script is complete I should have one zip file with a whole bunch of ".t" files in it. ... (2 Replies)
Discussion started by: dangral
2 Replies
Login or Register to Ask a Question