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 ... ( I know its Ruby code, but I know that Popen3 is popular with Perl. I'm sure a Perl user can answer this question as it more than likely isn't a problem with the language itself. )

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

I figured it out!

It wasn't that the program wasn't exiting. Ruby didn't know how to handle a nil variable in my code.

I changed my start_read function to:

def start_read( stdout , stderr )
  puts("Read thread is running ...")
  loop do
    # errorline = stderr.readline
    #puts("STDOUT #{errorline}")
    the_input = stdout.getc
    if the_input == nil                        # <=== This is what I added.
      Thread.kill(@write_thread)          #
      break                                          #
    end                                               #
    nodeSendData( @node_number, the_input.chr )
    STDOUT.flush
  end
end

So, when the program exits the input becomes nil. After discovering that it is nil, I killed the other thread. Everything works great now.

Thanks for nobody's help :)

- Matt

This question has already been answered. Start a new discussion instead.