Cannot obtain child process exit code on Windows

W

Wi siong Ko

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.
 
L

Luis Lavena

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?

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?
 
W

Wi siong Ko

Luis said:
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>


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>
 
L

Luis Lavena

Luis said:
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>

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>

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:

http://rubyinstaller.org/

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
 
R

Roger Pack

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
 
L

Lars Christensen

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?

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:
http://stackoverflow.com/questions/377407/detecting-how-a-batch-file-was-executed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top