# Return value for PTY.spawn

**URL:** <https://www.ruby-forum.com/t/return-value-for-pty-spawn/128894>\
**Category:** Ruby\
**Created:** [February 12, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/return-value-for-pty-spawn/128894 "2008-02-12T13:46:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 12, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/return-value-for-pty-spawn/128894/1 "2008-02-12T13:46:18Z")

</div>

Hi,  
I am a new bee to Ruby… facing problem with PTY.spawn command to  
execute ldapsearch query…  
I have 1.8.4 version of ruby  
here is the code i m trying to execute …

require ‘pty’  
require ‘expect’  
cmd = “ldapsearch -LLL -b “” -s one -H ldaps://Ip\_address -x -D  
cn=admin,o=org “o=org” CN description mail fullName  
generationQualifier givenName -W”

begin  
PTY.spawn(cmd) do |r, w, p |  
r.expect %r/Password.\*😕 do  
w.puts “password\r\n”  
end

```
            while ((str = r.gets) != nil)
                    puts str
            end
            end

    rescue PTY::ChildExited => e
         # raise unless e.is_a?(PTY::ChildExited)
           # pid = e.pid
            val = e.status
            print "return value ... #{val}\n"
            rescue Errno::EIO
    end

```

Here i want to know the exit status of ldapsearch command. Above  
code works fine if we give all information correct…  
When i tried printing wrong password onto the screen, i was  
expecting that block with “PTY::ChildExited” will be executed. For my  
surprise it didn’t happen…  
Here is the output with wrong password…

ldap\_bind: Invalid credentials (49)  
additional info: NDS error: failed authentication (-669)

(I expected that it will print “return value…some\_value”)

Can anybody help me in how to get the exit status of the command i  
executed…  
One more point i have noticed is that when i executed command “ls  
file\_does\_not\_exist” (Replace cmd with the ls cmd)… Here is what  
happening…

VIRTUAL1:/home/LATEST # ruby 3.rb  
return value … 512  
VIRTUAL1:/home/LATEST # ruby 3.rb  
ls: dasf: No such file or directory  
VIRTUAL1:/home/LATEST # ruby 3.rb  
return value … 512  
VIRTUAL1:/home/LATEST # ruby 3.rb  
return value … 512  
VIRTUAL1:/home/LATEST # ruby 3.rb  
ls: dasf: No such file or directory  
VIRTUAL1:/home/LATEST # ruby 3.rb  
return value … 512  
VIRTUAL1:/home/LATEST # ruby 3.rb  
return value … 512  
VIRTUAL1:/home/LATEST # ruby 3.rb  
ls: dasf: No such file or directory

We can notice that sometimes it is printing the message from  
ChildExited block and sometimes it is printing actual message…  
Here it seems to me we dont have any control over this… Is there any  
way to make our ruby program to always execute ChildExited block… ?

Thanks in advance… 🙂  
sayeed.
