Cannot obtain child process exit code on Windows

Hi,

I would like to obtain the exit code from a child program. But all the
methods I tried were in vain. Here are two files that i used to test.

C:\> type a.bat
@echo off
exit /b 29

C:\> type test.rb
system('a.bat')
puts $?
puts $? >> 8
exit

C:\>ruby test.rb
pid 5952 exit 0
0

Seems like in Windows, Ruby can never see the number 29 anyway.

Can anyone help me?
Thanks in advance.

···

--
Posted via http://www.ruby-forum.com/.

When reporting any issue, please include full version of Ruby (ruby -
v) that you're using.

Here an example:

ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]

irb(main):001:0> system("echo foo && exit /b 29")
foo
=> false
irb(main):002:0> $?
=> #<Process::Status: pid=2296,exited(29)>

irb(main):001:0> $?
=> #<Process::Status: pid=2180,exited(0)>
irb(main):002:0> system('foo.bat')
=> false
irb(main):003:0> $?
=> #<Process::Status: pid=2860,exited(29)>

The exitstatus is correct, what are you trying to achieve shifting the
Process:Status exitstatus value?

···

On Dec 16, 4:18 am, Wi siong Ko <weis...@hotmail.com> wrote:

Hi,

I would like to obtain the exit code from a child program. But all the
methods I tried were in vain. Here are two files that i used to test.

C:\> type a.bat
@echo off
exit /b 29

C:\> type test.rb
system('a.bat')
puts $?
puts $? >> 8
exit

C:\>ruby test.rb
pid 5952 exit 0
0

Seems like in Windows, Ruby can never see the number 29 anyway.

Can anyone help me?

--
Luis Lavena

C:\>ruby test.rb
pid 5952 exit 0
0

Seems like in Windows, Ruby can never see the number 29 anyway.

I bet ruby is giving you the exit code of running that .bat file under a
different instance of command. Maybe ping core it looks suspicious.
-r

···

--
Posted via http://www.ruby-forum.com/\.

This is normal, and doesn't have much to do with Ruby. You get the
same result in a plain command prompt. When running a batch script
from Ruby (or any other program), a new cmd.exe instance must be
spawned to launch the batch file:

C:\>type a.bat
@echo off
exit /B 29

C:\>cmd /c a.bat

C:\>echo %errorlevel%
0

When you run the batch file directly, you are not setting the exit
code, but simply the "errorlevel" of the current cmd instance. There
is no process exiting when running a.bat from the command line, so
there is no exit code to set by doing "exit /B 29".
Ruby gives you the exit code of the "process" - which is "cmd.exe",
and you are not asking "cmd.exe" to exit with any code other than 0.

You can change your .bat:

C:\>type b.bat
@echo off
exit 29

C:\>cmd /c b.bat

C:\>echo %errorlevel%
29

But this .bat will exit your console window if run from the command
line. This may help though:

···

On Wed, Dec 16, 2009 at 8:18 AM, Wi siong Ko <weishng@hotmail.com> wrote:

Hi,

I would like to obtain the exit code from a child program. But all the
methods I tried were in vain. Here are two files that i used to test.

C:\> type a.bat
@echo off
exit /b 29

C:\> type test.rb
system('a.bat')
puts $?
puts $? >> 8
exit

C:\>ruby test.rb
pid 5952 exit 0
0

Seems like in Windows, Ruby can never see the number 29 anyway.

Can anyone help me?

Luis Lavena wrote:

system('a.bat')
Can anyone help me?

When reporting any issue, please include full version of Ruby (ruby -
v) that you're using.

Here an example:

ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]

irb(main):001:0> system("echo foo && exit /b 29")
foo
=> false
irb(main):002:0> $?
=> #<Process::Status: pid=2296,exited(29)>

irb(main):001:0> $?
=> #<Process::Status: pid=2180,exited(0)>
irb(main):002:0> system('foo.bat')
=> false
irb(main):003:0> $?
=> #<Process::Status: pid=2860,exited(29)>

The exitstatus is correct, what are you trying to achieve shifting the
Process:Status exitstatus value?

Luis,

I am using ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-mswin32].
The right shifting was just trying with example given in
<http://ruby-doc.org/core-1.8.7/classes/Process/Status.html&gt;

Following your example, the exit code is 0 if is in a batch file. Here
is my session:

C:\>type foo.bat
exit /b 29

C:\>irb
irb(main):001:0> system("echo foo && exit /b 29")
foo
=> false
irb(main):002:0> $?
=> #<Process::Status: pid 5712 exit 29>
irb(main):003:0> system('foo.bat')

C:\>exit /b 29
=> true
irb(main):004:0> $?
=> #<Process::Status: pid 4488 exit 0>

···

On Dec 16, 4:18�am, Wi siong Ko <weis...@hotmail.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

Lars Christensen wrote:

You can change your .bat:

C:\>type b.bat
@echo off
exit 29

C:\>cmd /c b.bat

C:\>echo %errorlevel%
29

But this .bat will exit your console window if run from the command
line. This may help though:
windows - Detecting how a batch file was executed - Stack Overflow

Thank you for the explanation and solution. The cmd /c works great for
my purpose.

···

--
Posted via http://www.ruby-forum.com/\.

When testing if there is a bug, please try latest patchlevels.

Since you're using mswin32 installation and a really old patchlevel
(zero). Will recommend migrate to RubyInstaller:

Which is the successor of One-Click Installer and provides, at this
time under RC1, binaries for both 1.8.6 and 1.9.1-p243

···

On Dec 16, 5:32 am, Wi siong Ko <weis...@hotmail.com> wrote:

Luis Lavena wrote:
> On Dec 16, 4:18 am, Wi siong Ko <weis...@hotmail.com> wrote:
>> system('a.bat')
>> Can anyone help me?
> When reporting any issue, please include full version of Ruby (ruby -
> v) that you're using.

> Here an example:

> ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]

> irb(main):001:0> system("echo foo && exit /b 29")
> foo
> => false
> irb(main):002:0> $?
> => #<Process::Status: pid=2296,exited(29)>

> irb(main):001:0> $?
> => #<Process::Status: pid=2180,exited(0)>
> irb(main):002:0> system('foo.bat')
> => false
> irb(main):003:0> $?
> => #<Process::Status: pid=2860,exited(29)>

> The exitstatus is correct, what are you trying to achieve shifting the
> Process:Status exitstatus value?

Luis,

I am using ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-mswin32].
The right shifting was just trying with example given in
<http://ruby-doc.org/core-1.8.7/classes/Process/Status.html&gt;

Following your example, the exit code is 0 if is in a batch file. Here
is my session:

C:\>type foo.bat
exit /b 29

C:\>irb
irb(main):001:0> system("echo foo && exit /b 29")
foo
=> false
irb(main):002:0> $?
=> #<Process::Status: pid 5712 exit 29>
irb(main):003:0> system('foo.bat')

C:\>exit /b 29
=> true
irb(main):004:0> $?
=> #<Process::Status: pid 4488 exit 0>

--
Luis Lavena