views:

345

answers:

2

How do I redirect stderr and stdout to file for a ruby script?

+3  A: 
./yourscript.rb 2>&1 > log.txt

will redirect stdout and stderr to the same file.

Richard Fearn
Assuming you are running on *nix
Steve Weet
+4  A: 

From within a Ruby script, you can redirect stdout and stderr with the IO#reopen method.

# a.rb
$stdout.reopen("out.txt", "w")
$stderr.reopen("err.txt", "w")

puts 'normal output'
warn 'something to stderr'
$ ls
a.rb
$ ruby a.rb
$ ls
a.rb    err.txt out.txt
$ cat err.txt 
something to stderr
$ cat out.txt 
normal output
Mark Rushakoff