[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Invoking External Program

brian.kejser

9/3/2006 5:03:00 PM

Hi

I've been able to launch an external program and read standard output
using the following code.

IO.popen("program.exe", 'r+') do |io|
io.puts "command line input"
io.close_write
puts io.read
end

1) How would I add a timeout to the command (e.g. 10 seconds)?
2) How would I check the error level (e.g. 0, 1, etc)?
3) How would I read standard error?

Thanks






2 Answers

Jano Svitok

9/4/2006 2:23:00 AM

0

On 9/3/06, brian.kejser@protexis.com <brian.kejser@protexis.com> wrote:
> Hi
>
> I've been able to launch an external program and read standard output
> using the following code.
>
> IO.popen("program.exe", 'r+') do |io|
> io.puts "command line input"
> io.close_write
> puts io.read
> end
>
> 1) How would I add a timeout to the command (e.g. 10 seconds)?
> 2) How would I check the error level (e.g. 0, 1, etc)?
> 3) How would I read standard error?

Just quick, uncomplete answers (from .exe I guess you are on windows):

1. try using Timeout.timeout [1] but I'm not sure how successful
you'll be - when the program blocks then you probably won't be able to
do anything. When the timeout elapses, you'd need to kill the process.

2. use $? [2]

3. look for popen3 [3] or redirect stderr to stdout or a file. I'm not
sure whether popen3 works on windows.

[1] http://ruby-doc.org/core/classes/Timeout.ht...
[2] http://www.zenspider.com/Languages/Ruby/QuickR...
[3] http://ruby-doc.org/stdlib/libdoc/open3/rdoc/...

brian.kejser

9/4/2006 11:17:00 PM

0

Hi

Comments made inline ....

-----Original Message-----
From: "Jan Svitok" <jan.svitok@gmail.com>
Sent: Sun, September 3, 2006 19:23
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Subject: Re: Invoking External Program

On 9/3/06, brian.kejser@protexis.com <brian.kejser@protexis.com> wrote:
> Hi
>
> I've been able to launch an external program and read standard output
> using the following code.
>
> IO.popen("program.exe", 'r+') do |io|
> io.puts "command line input"
> io.close_write
> puts io.read
> end
>
> 1) How would I add a timeout to the command (e.g. 10 seconds)?
> 2) How would I check the error level (e.g. 0, 1, etc)?
> 3) How would I read standard error?



> Just quick, uncomplete answers (from .exe I guess you are on windows):

> 1. try using Timeout.timeout [1] but I'm not sure how successful
> you'll be - when the program blocks then you probably won't be able to
> do anything. When the timeout elapses, you'd need to kill the process.

Thanks. It worked.

> 2. use $? [2]

It didn't work, however, the following code did.

exitstatus = Process.waitpid2(io.pid)[1].exitstatus

Are there any problems with getting the exit status by this means?

> 3. look for popen3 [3] or redirect stderr to stdout or a file. I'm not
> sure whether popen3 works on windows.

> [1] http://ruby-doc.org/core/classes/Timeout.ht...
> [2] http://www.zenspider.com/Languages/Ruby/QuickR...
> [3] http://ruby-doc.org/stdlib/libdoc/open3/rdoc/...

Yeah. It doesn't look like popen3 is supported on windows.

Thanks