# IO.popen exit the program when it failed

**URL:** <https://www.ruby-forum.com/t/io-popen-exit-the-program-when-it-failed/192255>\
**Category:** Ruby\
**Created:** [July 27, 2010, 5:17am UTC](https://www.ruby-forum.com/t/io-popen-exit-the-program-when-it-failed/192255 "2010-07-27T05:17:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [July 27, 2010, 5:17am UTC](https://www.ruby-forum.com/t/io-popen-exit-the-program-when-it-failed/192255/1 "2010-07-27T05:17:02Z")

</div>

i have been trying to recieved the output of a command with IO.popen and  
it works great but if i place a unknown command to the system the ruby  
program terminates.

def comando ( inputcmd )  
f = IO.popen(inputcmd)  
output = f.readlines  
return output  
end

#########################################################  
there is a loop calling the method like this:  
#########################################################

```
  cliente.puts "Escriba el comando a ejecutar por cmd.exe: "
  inputcmd = cliente.gets
  cliente.puts comando( inputcmd )

```

any help will be appreciated!!

Thank you!

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [July 27, 2010, 5:41am UTC](https://www.ruby-forum.com/t/io-popen-exit-the-program-when-it-failed/192255/2 "2010-07-27T05:41:52Z")

</div>

i found the solution on the forum 😃

i use: IO.popen(cmdline + " 2\>&1", “w+”)

but now i have another problem, if i run a command that needs several  
lines  
the output comes back incomplete.  
Any ideas?

there is a way to increase the size of the  
buffer? does it affect if the variable its been trransfer with  
TCPSockets??

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [July 27, 2010, 1:59pm UTC](https://www.ruby-forum.com/t/io-popen-exit-the-program-when-it-failed/192255/3 "2010-07-27T13:59:03Z")

</div>

Daniel Flores wrote:

> i found the solution on the forum 😃
> 
> i use: IO.popen(cmdline + " 2\>&1", “w+”)
> 
> but now i have another problem, if i run a command that needs several  
> lines  
> the output comes back incomplete.  
> Any ideas?

use .read instead of .gets (?)

-r
