# Capturing stderr from Kernel.exec

**URL:** <https://www.ruby-forum.com/t/capturing-stderr-from-kernel-exec/68097>\
**Category:** Ruby\
**Created:** [September 9, 2006, 5:39am UTC](https://www.ruby-forum.com/t/capturing-stderr-from-kernel-exec/68097 "2006-09-09T05:39:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [September 9, 2006, 5:39am UTC](https://www.ruby-forum.com/t/capturing-stderr-from-kernel-exec/68097/1 "2006-09-09T05:39:43Z")

</div>

Hello,

I’m trying to run some simple commands using Kernel.exec but need to  
capture the stderr for them. Is there an (easy) way to do it with  
just ruby and the standard library or should I get open4?

Thanks,  
-carl

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [September 9, 2006, 6:07am UTC](https://www.ruby-forum.com/t/capturing-stderr-from-kernel-exec/68097/2 "2006-09-09T06:07:47Z")

</div>

On Sat, 9 Sep 2006, Carl L. wrote:

> Hello,
> 
> I’m trying to run some simple commands using Kernel.exec but need to capture  
> the stderr for them. Is there an (easy) way to do it with just ruby and the  
> standard library or should I get open4?
> 
> Thanks,  
> -carl

open4 adds quite a bit to open3, which is in the stdlib. so depending  
on your  
exact needs you may or may not need it.

however, by capture i assume you are on unix, since otherwise you cannot  
fork  
and exec just replaces the calling process so theres nothing to capture.  
that  
being the case you can just

out\_err = ‘’

IO.popen("#{ cmd } 2\>&1") do |pipe|  
while((line = pipe.gets))  
out\_err \<\< line  
end  
end

if you need to separate them you can do

require ‘open3’

stdout, stderr = ‘’, ‘’

Open3.popen3(cmd) do |i,o,e|  
i.close

```
 while((line = o.gets))
   stdout << line
 end

 while((line = e.gets))
   stderr << line
 end

```

end

open4, as mentioned, does a whole lot more - check out the docs/samples.

regards.

-a

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [September 9, 2006, 6:09am UTC](https://www.ruby-forum.com/t/capturing-stderr-from-kernel-exec/68097/3 "2006-09-09T06:09:47Z")

</div>

Carl L. wrote:

> Hello,
> 
> I’m trying to run some simple commands using Kernel.exec but need to  
> capture the stderr for them. Is there an (easy) way to do it with  
> just ruby and the standard library or should I get open4?
> 
> Thanks,  
> -carl

Kernel#exec never returns control; it replaces the ruby process with  
the one you pass it. You need open3 (stdlib) or popen4.

[1] [http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html)  
[2] [http://popen4.rubyforge.org/](http://popen4.rubyforge.org/)

Regards,  
Jordan
