Spawning a process in ruby, capturing stdout, stderr, getting status status

I want to run an executable from a ruby ​​rake script, let's say foo.exe

I want to outputs STDOUTand STDERRfrom foo.exerecorded directly in the console. I run the rake command.

When the process ends, I want to write the exit code to a variable. How to achieve this?

I've played with backticks, process.spawn, systembut I can not get all the behavior that I want only part

Update: I'm on Windows at the standard command prompt, not cygwin

+5
source share
2 answers

systemgets the right behavior STDOUT. It also returns truefor a null exit code, which may be useful.

$? system, :

system 'foo.exe'
$?.exitstatus

Runner.execute_command .

+8

stdout

foo.exe , - - cygwin? script unixy, :

result = `foo.exe 2>&1`
status = $?.exitstatus

googling , Windows, assupmtion

+2

All Articles