# Open3

**URL:** <https://www.ruby-forum.com/t/open3/155595>\
**Category:** Ruby\
**Created:** [December 14, 2008, 10:37am UTC](https://www.ruby-forum.com/t/open3/155595 "2008-12-14T10:37:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [December 14, 2008, 10:37am UTC](https://www.ruby-forum.com/t/open3/155595/1 "2008-12-14T10:37:26Z")

</div>

Hello everyone. I have created a sales system for my company. Basically  
you telnet to the system and enter your login name, password … From  
there you can access the sales system.

I am working on a feature to write reports for a certain area of the  
system. For this, I want it to just shell out to VIM (Vi iMproved). For  
this I will be using Popen3, so the telnet user can access the sytem.

This is some example code I have …

def runVimForReport( node\_number, file\_name )  
stdin, stdout, stderr = Open3.popen3(‘vim #{file\_name}’ )

read\_thread = Thread.new { start\_read(stdout,stderr) }  
write\_thread = Thread.new { start\_write(stdin,stderr) }

write\_thread.join  
read\_thread.join  
end

def start\_read( stdout , stderr )  
loop do  
the\_input = stdout.getc  
nodeSendData( @node\_number, the\_input.chr )  
STDOUT.flush  
end

puts(“EXIT READ THREAD”)  
end

def start\_write( stdin , stderr )  
loop do  
STDOUT.flush  
input = nodeGetChar( @node\_number )  
stdin.putc(input)  
end  
end

This code works well, the user can access VIM just fine. There is only  
one problem …

When the user exits the program it continues to read/write to the  
process. It doesn’t just simply exit the functions. How do I tell when a  
program has exited? I tried other programs and all are the same … It  
just hangs. The user has to logout and log back in. Even when  
disconnecting the telnet session, the program is still running in the  
background.

Is there a variable I can check in my loop to see if the program has  
exited?

Thanks for all your help!

- Matt

---

<div class="post-metadata">

**Author:** ![vanderkerkoff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7feea3/32.png) [@vanderkerkoff](https://www.ruby-forum.com/u/vanderkerkoff)\
**Post date:** [December 14, 2008, 11:35am UTC](https://www.ruby-forum.com/t/open3/155595/2 "2008-12-14T11:35:27Z")

</div>

Got it fixed. It wasn’t that the program wasn’t exiting it was that the  
ruby program. I added this to my start\_read thread.

```
if the_input == nil
  Thread.kill(@write_thread)
  break
end
```
