Open3


#1

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

#2

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