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

P

Phil Rhoades

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 Rhoades

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: (e-mail address removed)
 
E

Eric Hodel

This seems to work:

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
 
R

Rick Tessner

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.

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top