# Popen hangs out for user input

**URL:** <https://www.ruby-forum.com/t/popen-hangs-out-for-user-input/196166>\
**Category:** Ruby\
**Created:** [October 7, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/popen-hangs-out-for-user-input/196166 "2010-10-07T20:43:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![felix.leg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c89c15/32.png) [@felix.leg](https://www.ruby-forum.com/u/felix.leg)\
**Post date:** [October 7, 2010, 8:43pm UTC](https://www.ruby-forum.com/t/popen-hangs-out-for-user-input/196166/1 "2010-10-07T20:43:59Z")

</div>

Hi,

I want to use Ruby with latex,so I wrote a simple program which waits  
for “\typein” command called from pdflatex:

`
#!/usr/bin/ruby
file = IO.popen(‘pdflatex -interaction scrollmode test.tex’,‘r+’) #1
until file.readline.include? ‘typein’; end
file.write “a line from RUBY”
file.readlines #2
file.close
`

If I run the command from #1 in a sheel it works right: scrolls  
“standard” latex asks, and stops for my “@typein=”. After I answer it  
continues normally.

But when I want do the same in Ruby with above code, it hangs out  
waiting for input. It looks like #2 causes this, but I don’t know why…

---

<div class="post-metadata">

**Author:** ![felix.leg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c89c15/32.png) [@felix.leg](https://www.ruby-forum.com/u/felix.leg)\
**Post date:** [October 7, 2010, 9:08pm UTC](https://www.ruby-forum.com/t/popen-hangs-out-for-user-input/196166/2 "2010-10-07T21:08:35Z")

</div>

On 10/7/2010 1:40 PM, felix.leg wrote:

> until file.readline.include? ‘typein’; end  
> waiting for input. It looks like #2 causes this, but I don’t know why…  
> It’s probably because the write buffer for the pipe is not being  
> flushed. My preferred option to fix this sort of thing is to make the  
> file object synchronous:

file = IO.popen(…)  
file.sync = true

However, you might prefer to explicitly flush things instead:

file.write(…)  
file.flush  
…  
file.write(…)  
file.write(…)  
file.flush

-Jeremy

---

<div class="post-metadata">

**Author:** ![felix.leg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c89c15/32.png) [@felix.leg](https://www.ruby-forum.com/u/felix.leg)\
**Post date:** [October 7, 2010, 9:22pm UTC](https://www.ruby-forum.com/t/popen-hangs-out-for-user-input/196166/3 "2010-10-07T21:22:02Z")

</div>

On 10/7/2010 2:07 PM, Jeremy B. wrote:

> > But when I want do the same in Ruby with above code, it hangs out
> 
> file.write(…)  
> file.flush  
> …  
> file.write(…)  
> file.write(…)  
> file.flush

One more thing: you may need to insert a newline at the end of the  
strings you write or use #puts instead of #write. Most interactive  
programs like this expect a newline character to flag the end of data  
entry for a field.

-Jeremy

---

<div class="post-metadata">

**Author:** ![felix.leg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c89c15/32.png) [@felix.leg](https://www.ruby-forum.com/u/felix.leg)\
**Post date:** [October 8, 2010, 11:32am UTC](https://www.ruby-forum.com/t/popen-hangs-out-for-user-input/196166/4 "2010-10-08T11:32:56Z")

</div>

On Thu, Oct 7, 2010 at 9:21 PM, Jeremy B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > file = IO.popen(‘pdflatex -interaction scrollmode test.tex’,‘r+’) #1
> > 
> > However, you might prefer to explicitly flush things instead:  
> > programs like this expect a newline character to flag the end of data  
> > entry for a field.

Another reasonable thing to do is to close the write end of the pipe  
if you are done. Note also that expect helps working with interactive  
programs. And, of course, use the block form of IO.popen.

require ‘expect’

IO.popen(‘pdflatex -interaction scrollmode test.tex’,‘r+’) do |file|  
file.expect /typein/, 10 do  
file.puts “a line from RUBY”  
end  
file.close\_write  
file.each {} # read until EOF so process can do what it needs to  
end

Btw, in 1.9 you can pass the command line as an array, i.e.

IO.popen(%w{pdflatex -interaction scrollmode test.tex},‘r+’) do |file|

file\_name = “test.tex” # possibly read from ARGV  
IO.popen(%w{pdflatex -interaction scrollmode} \<\< file\_name, ‘r+’) do  
|file|

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![felix.leg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c89c15/32.png) [@felix.leg](https://www.ruby-forum.com/u/felix.leg)\
**Post date:** [October 8, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/popen-hangs-out-for-user-input/196166/5 "2010-10-08T19:30:36Z")

</div>

W dniu 07.10.2010 21:21, Jeremy B. pisze:

> > > file = IO.popen(‘pdflatex -interaction scrollmode test.tex’,‘r+’) #1
> > 
> > However, you might prefer to explicitly flush things instead:  
> > programs like this expect a newline character to flag the end of data  
> > entry for a field.
> 
> -Jeremy

Both your answers works. THX!
