# Net::openssh - how to get process exit status?

**URL:** <https://www.ruby-forum.com/t/net-openssh-how-to-get-process-exit-status/149626>\
**Category:** Ruby\
**Created:** [September 26, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/net-openssh-how-to-get-process-exit-status/149626 "2008-09-26T22:47:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [September 26, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/net-openssh-how-to-get-process-exit-status/149626/1 "2008-09-26T22:47:10Z")

</div>

Hi,

I am using net-ssh 2.0.1. I have not been able to figure out how to get  
the exit value from a remotely executed process. In the only examples I  
could find, only the stdout was returned, like this one:

Net::SSH.start(host, username, :password =\> password) do |ssh|  
print ssh.exec!(“hostname”)  
end

I would like to check to see if the remote command returns a non-zero  
value to indicate failure.

Thanks,

Ryan

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [September 29, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/net-openssh-how-to-get-process-exit-status/149626/2 "2008-09-29T19:16:10Z")

</div>

On Sep 26, 2008, at 1:46 PM, Ryan Grow wrote:

> I am using net-ssh 2.0.1. I have not been able to figure out how to  
> get  
> the exit value from a remotely executed process. In the only  
> examples I  
> could find, only the stdout was returned, like this one:
> 
> Net::SSH.start(host, username, :password =\> password) do |ssh|  
> print ssh.exec!(“hostname”)  
> end

If you want the process exit status, you have to forgo the ssh.exec!()  
convenience method and work at a lower level. Here’s how to do it:

```
 ssh.open_channel do |channel|
   channel.exec(command) do |ch, success|
     unless success
       abort "FAILED: couldn't execute command (ssh.channel.exec

```

failure)"  
end

```
     channel.on_data do |ch, data| # stdout
       print data
     end

     channel.on_extended_data do |ch, type, data|
       next unless type == 1 # only handle stderr
       $stderr.print data
     end

     channel.on_request("exit-status") do |ch, data|
       exit_code = data.read_long
       if exit_code > 0
         puts "ERROR: exit code #{exit_code}"
       else
         puts "success"
       end
     end

     channel.on_request("exit-signal") do |ch, data|
       puts "SIGNAL: #{data.read_long}"
     end
   end
 end
 ssh.loop

```

–Young
