capturing stderr for a file

T

Ted Flethuseo

Hi everyone,

I am trying to run a script from ruby, but it produces some output to
STDERR. It isn't capturing that output. How do I capture it?

file = File.open("my.txt", "w")

arr.each do |e|
file.puts `./script.sh #{e}`
end

Ted.
 
A

Avdi Grimm

I am trying to run a script from ruby, but it produces some output to
STDERR. It isn't capturing that output. How do I capture it?

Two options:

1. Redirect STDOUT to STDERR in the command:

output = `./script.sh #{e} 2>&1`

2. Use Open3 to capture the error stream:

require 'open3'
Open3.popen3("./script.sh #{e} 2>&1") do |i, o, e|
puts "STDOUT: #{o.read}"
puts "STDERR: #{e.read}"
end
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top