[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Return code of process from IO

Farrel Lifson

3/22/2006 8:10:00 PM

Hi folks,

I'm using IO.popen to communicate with an external process. At some
point the external process will exit either with a true or false
condition. It uses shell return codes (0 for true, 1 for false) to
indicate that this is happened. Is there a way to get the vale of this
return code from an IO object? Or will it be better to use Kernel.` or
suchlike. The $? system var doesn't seem to be updated using IO.

Farrel


5 Answers

Farrel Lifson

3/22/2006 8:36:00 PM

0

Just solved it. Just run Process.wait after the call to the IO.popen
and $? gets updated.

On 3/22/06, Farrel Lifson <farrel.lifson@gmail.com> wrote:
> Hi folks,
>
> I'm using IO.popen to communicate with an external process. At some
> point the external process will exit either with a true or false
> condition. It uses shell return codes (0 for true, 1 for false) to
> indicate that this is happened. Is there a way to get the vale of this
> return code from an IO object? Or will it be better to use Kernel.` or
> suchlike. The $? system var doesn't seem to be updated using IO.
>
> Farrel
>


Shea Martin

3/22/2006 9:03:00 PM

0

Farrel Lifson wrote:
> Just solved it. Just run Process.wait after the call to the IO.popen
> and $? gets updated.

This doesn't work in this case:

#!/usr/bin/ruby

Thread.new{
IO.popen('ls non_existant_dir').each_line{|l| puts l}
sleep 10
Process.wait
puts "exit status is #{$?}, should be non-zero"
}

sleep 5

Thread.new{
IO.popen('dir').each_line{|l| puts l}
Process.wait
puts "exit status is #{$?}, should be 0"
}

sleep 15

exit 0


~S

Farrel Lifson

3/22/2006 9:09:00 PM

0

Yeah that can be a problem, but luckily I'm not using any threads so
it seems to be working...

On 3/22/06, Shea Martin <null@void.0> wrote:
> Farrel Lifson wrote:
> > Just solved it. Just run Process.wait after the call to the IO.popen
> > and $? gets updated.
>
> This doesn't work in this case:
>
> #!/usr/bin/ruby
>
> Thread.new{
> IO.popen('ls non_existant_dir').each_line{|l| puts l}
> sleep 10
> Process.wait
> puts "exit status is #{$?}, should be non-zero"
> }
>
> sleep 5
>
> Thread.new{
> IO.popen('dir').each_line{|l| puts l}
> Process.wait
> puts "exit status is #{$?}, should be 0"
> }
>
> sleep 15
>
> exit 0
>
>
> ~S
>
>


Shea Martin

3/22/2006 9:09:00 PM

0

Shea Martin wrote:
> Farrel Lifson wrote:
>> Just solved it. Just run Process.wait after the call to the IO.popen
>> and $? gets updated.
>
> This doesn't work in this case:
>
> #!/usr/bin/ruby
>
> Thread.new{
> IO.popen('ls non_existant_dir').each_line{|l| puts l}
> sleep 10
> Process.wait
> puts "exit status is #{$?}, should be non-zero"
> }
>
> sleep 5
>
> Thread.new{
> IO.popen('dir').each_line{|l| puts l}
> Process.wait
> puts "exit status is #{$?}, should be 0"
> }
>
> sleep 15
>
> exit 0
>
>
> ~S

This should work in a mt env.

IO.popen('dir') { |p|
p.each_line{ |l| puts l }
puts "exitstatus is #{Process.waitpid(p.pid)}"
}

Shea Martin

3/22/2006 9:19:00 PM

0

Shea Martin wrote:
> Shea Martin wrote:
>> Farrel Lifson wrote:
>>> Just solved it. Just run Process.wait after the call to the IO.popen
>>> and $? gets updated.
>>
>> This doesn't work in this case:
>>
>> #!/usr/bin/ruby
>>
>> Thread.new{
>> IO.popen('ls non_existant_dir').each_line{|l| puts l}
>> sleep 10
>> Process.wait
>> puts "exit status is #{$?}, should be non-zero"
>> }
>>
>> sleep 5
>>
>> Thread.new{
>> IO.popen('dir').each_line{|l| puts l}
>> Process.wait
>> puts "exit status is #{$?}, should be 0"
>> }
>>
>> sleep 15
>>
>> exit 0
>>
>>
>> ~S
>
> This should work in a mt env.
>
> IO.popen('dir') { |p|
> p.each_line{ |l| puts l }
> puts "exitstatus is #{Process.waitpid(p.pid)}"
> }
that should be #{Process.waitpid2(p.pid)[1].exitstatus}