# App not waiting for popen to finish

**URL:** <https://www.ruby-forum.com/t/app-not-waiting-for-popen-to-finish/57461>\
**Category:** Ruby\
**Created:** [April 11, 2006, 7:33am UTC](https://www.ruby-forum.com/t/app-not-waiting-for-popen-to-finish/57461 "2006-04-11T07:33:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSMKSM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@SSMKSM](https://www.ruby-forum.com/u/SSMKSM)\
**Post date:** [April 11, 2006, 7:33am UTC](https://www.ruby-forum.com/t/app-not-waiting-for-popen-to-finish/57461/1 "2006-04-11T07:33:01Z")

</div>

Hello,

I’m using ‘wget’ to download a few files and then tar them up. I’m  
using popen to make calls to wget and tar. Problem is that it tries to  
call tar right after the wget call. I need to wait for wget to finish.  
Here is what i’ve got so far.

categories = […]

categories.each\_with\_index { |url,i|  
IO.popen(“wget -r -np -l 0 -w 4 #{url}”)  
IO.popen(“tar -zcf public\_html/category\_#{i}.tar.gz files”)  
}

Thanks in advance.

Ricardo

---

<div class="post-metadata">

**Author:** ![SSMKSM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@SSMKSM](https://www.ruby-forum.com/u/SSMKSM)\
**Post date:** [April 11, 2006, 7:48am UTC](https://www.ruby-forum.com/t/app-not-waiting-for-popen-to-finish/57461/2 "2006-04-11T07:48:15Z")

</div>

On Apr 11, 2006, at 1:31 AM, |MKSM| wrote:

> IO.popen(“wget -r -np -l 0 -w 4 #{url}”)  
> IO.popen(“tar -zcf public\_html/category\_#{i}.tar.gz files”)  
> }
> 
> Thanks in advance.
> 
> Ricardo

The solution would be to do

IO.popen("…")  
Process.wait

However since you aren’t apparently even using the IO capabilities of  
popen, why not simply make use of system? It will automatically wait  
for the process to exit  
e.g.

system(“wget -r -np -l 0 -w 4 #{url}”)  
Of course if the only reason you are using IO.popen is to prevent  
output to the console you can do  
system(“wget -r -np -l 0 -w 4 #{url} 2\>&1 \>/dev/null”)  
Or even:

Process.wait Process.fork { STDOUT.reopen("/dev/null", “w”);  
STDERR.reopen("/dev/null", “w”); exec(“wget -r -np -l 0 -w 4 #{url}”) }

---

<div class="post-metadata">

**Author:** ![SSMKSM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@SSMKSM](https://www.ruby-forum.com/u/SSMKSM)\
**Post date:** [April 11, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/app-not-waiting-for-popen-to-finish/57461/3 "2006-04-11T15:35:37Z")

</div>

On Tue, 11 Apr 2006, |MKSM| wrote:

> IO.popen(“wget -r -np -l 0 -w 4 #{url}”)  
> here you have a pipe that’s been returned. wget is writing to it. you  
> simply  
> need to read all of wget’s output and close the pipe:

IO.popen(“wget -r -np -l 0 -w 4 #{url}”){|pipe| pipe.read}

> IO.popen(“tar -zcf public\_html/category\_#{i}.tar.gz files”)  
> }
> 
> Thanks in advance.
> 
> Ricardo

-a

---

<div class="post-metadata">

**Author:** ![SSMKSM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@SSMKSM](https://www.ruby-forum.com/u/SSMKSM)\
**Post date:** [April 11, 2006, 4:03pm UTC](https://www.ruby-forum.com/t/app-not-waiting-for-popen-to-finish/57461/4 "2006-04-11T16:03:18Z")

</div>

Thanks for your suggestion. I’m going with the last one using a block  
with popen. That way i can easily send the output to a logfile.

Best Regards,

Ricardo

---

<div class="post-metadata">

**Author:** ![SSMKSM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc8723/32.png) [@SSMKSM](https://www.ruby-forum.com/u/SSMKSM)\
**Post date:** [April 11, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/app-not-waiting-for-popen-to-finish/57461/5 "2006-04-11T16:18:42Z")

</div>

On Tue, 11 Apr 2006, |MKSM| wrote:

> Thanks for your suggestion. I’m going with the last one using a block  
> with popen. That way i can easily send the output to a logfile.
> 
> Best Regards,

fyi. you still need to call Process.wait if you also want the status.  
with  
my session lib you can do this as

require ‘session’

sh = Session::SH::new

stdout, stderr = ‘’, ‘’  
append = lambda{|o,e| stdout \<\< o if o; stderr \<\< e if e}

status =  
sh.execute(“wget -r -np -l 0 -w 4 #{url}”, &append)

status =  
sh.execute(“tar -zcf public\_html/category\_#{i}.tar.gz files”,  
&append)

where stdout and stderr can be anything that supports ‘\<\<’. the above  
starts  
only one child process as well.

regards.

-a
