# How to know if $stderr is reopened to /dev/null

**URL:** <https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683>\
**Category:** Ruby\
**Created:** [January 16, 2010, 12:30am UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683 "2010-01-16T00:30:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 16, 2010, 12:30am UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683/1 "2010-01-16T00:30:39Z")

</div>

Hi, how could I know if $stderr has been redirected to /dev/null?  
This is, imagine a program does:

$stderr.reopen “/dev/null”

And later I want to check if it’s redirected to /dev/null or not.

I’ve found a method which could work:

irb $stderr.inspect  
“#IO:/dev/null”

So I could parse the resulting string. However it doesn’t seem a very  
“cool”  
approach. Is there any there any other more ellegant way?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 16, 2010, 12:42am UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683/2 "2010-01-16T00:42:21Z")

</div>

IÃ±aki Baz C. wrote:

> “#IO:/dev/null”
> 
> So I could parse the resulting string. However it doesn’t seem a very “cool”  
> approach. Is there any there any other more ellegant way?
> 
> Thanks a lot.

You can use $stderr.tty? if you just need to know if a terminal is  
listening.

-Justin

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 16, 2010, 12:45am UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683/3 "2010-01-16T00:45:52Z")

</div>

El SÃ¡bado, 16 de Enero de 2010, Justin C. escribiÃ³:

> > irb $stderr.inspect  
> > “#IO:/dev/null”
> > 
> > So I could parse the resulting string. However it doesn’t seem a very  
> > “cool” approach. Is there any there any other more ellegant way?
> > 
> > Thanks a lot.
> 
> You can use $stderr.tty? if you just need to know if a terminal is  
> listening.

Great! It also works in case $stderr is redirected to a file in which  
case  
$stderr.tty? also returns false 🙂

Thanks.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 16, 2010, 12:50am UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683/4 "2010-01-16T00:50:33Z")

</div>

El Sábado, 16 de Enero de 2010, Steven P.  
escribió:\> \> Hi, how could I know if $stderr has been redirected to /dev/null?

> On anything sufficiently Unix-like, you could fstat the filehandle and stat  
> the path (/dev/null) and check the major and minor device numbers.

Thanks. However $stderr.tty? seems simpler for my requeriments 🙂

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 16, 2010, 12:47am UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683/5 "2010-01-16T00:47:37Z")

</div>

> Hi, how could I know if $stderr has been redirected to /dev/null?

On anything sufficiently Unix-like, you could fstat the filehandle and  
stat the path (/dev/null) and check the major and minor device numbers.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 16, 2010, 12:58am UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683/6 "2010-01-16T00:58:45Z")

</div>

On 2010-01-15, Iñaki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, how could I know if $stderr has been redirected to /dev/null?

You couldn’t.

Consider a wrapper:

exec 5\>/dev/null  
ruby foo.rb 2\>&5

Or for that matter, a file “/home/ibc/nulldev” which happens to be  
a character special device using the same driver as /dev/null.

… Note that this assumes that what you care about is “$stderr is  
being thrown away”, not the exact path by which it’s being done.

-s

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 16, 2010, 1:02am UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683/7 "2010-01-16T01:02:15Z")

</div>

El Sábado, 16 de Enero de 2010, Seebs  
escribió:\> Or for that matter, a file “/home/ibc/nulldev” which happens to be

> a character special device using the same driver as /dev/null.
> 
> … Note that this assumes that what you care about is “$stderr is  
> being thrown away”, not the exact path by which it’s being done.

Thanks for the clarification. However it’s enough for me using  
$stderr.tty?.  
This is, I just care when $stederr is not pointing to a terminal.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [January 16, 2010, 5:50pm UTC](https://www.ruby-forum.com/t/how-to-know-if-stderr-is-reopened-to-dev-null/181683/8 "2010-01-16T17:50:35Z")

</div>

On 01/16/2010 01:01 AM, Iñaki Baz C. wrote:

> > a character special device using the same driver as /dev/null.
> > 
> > … Note that this assumes that what you care about is “$stderr is  
> > being thrown away”, not the exact path by which it’s being done.
> 
> Thanks for the clarification. However it’s enough for me using $stderr.tty?.  
> This is, I just care when $stederr is not pointing to a terminal.

Note though, that this also includes cases where stderr is redirected to  
a pipe.

robert@fussel:~$ ruby19 -e ‘p $stderr.tty?’  
true  
robert@fussel:~$ ruby19 -e ‘p $stderr.tty?’ 2\>&1 | cat  
false  
robert@fussel:~$

Kind regards

robert
