# Ruby equivalent to "exec \> $logfile 2\>&1" in sh script?

**URL:** <https://www.ruby-forum.com/t/ruby-equivalent-to-exec-logfile-2-1-in-sh-script/77868>\
**Category:** Ruby\
**Created:** [December 1, 2006, 4:00am UTC](https://www.ruby-forum.com/t/ruby-equivalent-to-exec-logfile-2-1-in-sh-script/77868 "2006-12-01T04:00:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 1, 2006, 4:00am UTC](https://www.ruby-forum.com/t/ruby-equivalent-to-exec-logfile-2-1-in-sh-script/77868/1 "2006-12-01T04:00:34Z")

</div>

People,

I quite frequently have something like:

```
exec > $logfile 2>&1

```

at the top of my shell scripts to output everything that follows  
(including  
errors) into a log file - is there some way of doing the equivalent in a  
Ruby  
script?

Thanks,

## Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Mobile: +61:(0)411-185-652  
Fax: +61:(0)2-8221-9599  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 1, 2006, 4:24am UTC](https://www.ruby-forum.com/t/ruby-equivalent-to-exec-logfile-2-1-in-sh-script/77868/2 "2006-12-01T04:24:54Z")

</div>

Phil R. wrote:

> People,
> 
> I quite frequently have something like:
> 
> exec \> $logfile 2\>&1
> 
> at the top of my shell scripts to output everything that follows  
> (including  
> errors) into a log file - is there some way of doing the equivalent in a  
> Ruby  
> script?
> 
> Thanks,
> 
> Phil.

This seems to work:

outfile = File.open(“output.txt”, “w”)  
$stdout.reopen outfile  
$stderr.reopen outfile

puts “hello world!”  
system(“dir no\_exist”)

=== output.txt ===

hello world!  
Volume in drive C has no label.  
Volume Serial Number is 7874-56C8

Directory of C:  
File Not Found

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 1, 2006, 8:12am UTC](https://www.ruby-forum.com/t/ruby-equivalent-to-exec-logfile-2-1-in-sh-script/77868/3 "2006-12-01T08:12:53Z")

</div>

On Nov 30, 2006, at 19:24 , El Gato wrote:

> outfile = File.open(“output.txt”, “w”)  
> $stdout.reopen outfile  
> $stderr.reopen outfile
> 
> puts “hello world!”  
> system(“dir no\_exist”)

Not really.

$ ruby  
outfile = File.open(“output.txt”, “w”)  
$stdout.reopen outfile  
$stderr.reopen outfile

0.upto 10 do |v| (v%2==0 ? STDOUT : STDERR).puts v; end  
$ cat output.txt  
1  
3  
5  
7  
9  
0  
2  
4  
6  
8  
10

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![philippeqc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@philippeqc](https://www.ruby-forum.com/u/philippeqc)\
**Post date:** [December 1, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/ruby-equivalent-to-exec-logfile-2-1-in-sh-script/77868/4 "2006-12-01T16:38:24Z")

</div>

Hi all,

Adding $stdout.sync = true; $stderr.sync = true should solve the problem  
of stdout and stderr being output of of sync.

See in-line edit below.

On Fri, 2006-01-12 at 16:10 +0900, Eric H. wrote:

> $ ruby  
> outfile = File.open(“output.txt”, “w”)  
> $stdout.reopen outfile  
> $stderr.reopen outfile

$stdout.sync = true  
$stderr.sync = true
