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
2010-06-10 21:19:37
Assuming you are running on *nix
Steve Weet
2010-06-11 06:50:05
+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
2010-06-10 21:38:12