2

I'm trying to run a Ruby application, which outputs information to the console. I'd like to make this output get saved to a text/log file. Is this even possible? There must be a flag for ruby to do this right?

1
  • Do you want to do this within the Ruby application, or within the terminal that runs the Ruby application? Commented Jan 3, 2012 at 23:39

1 Answer 1

6

Use shell redirections:

ruby script.rb > out.txt
rails server > out.txt

On another note, you may also redirect $stdout, $stderr:

$stdout = File.new('/path/to/out.txt', 'w')

http://eddymulyono.livejournal.com/65791.html

Sign up to request clarification or add additional context in comments.

6 Comments

this does work, but only after the script has been excecuted. I would like to see the output as it occurs (as this txt file is being loaded into a web page to view the console)
i'm a noob at ruby, the script outputs it's data using puts. How will changing stdout change where puts goes? Sorry for this.
Just looked at your link, but it doesn't seem to be making these files that a specify.
@nickw444 Because puts writes to stdout.
script -c "rails runner -e development lib/scripts/my_script.rb" report.txt helped me to capture a Rails runner script's very-very long output easily to a file. I tried using redirecting to a file but it got written only at the end of script. That didn't helped me because I had few interactive commands in my script. Then I used just script on my and then ran the rails runner in script session but it didn't wrote everything. Then I found this script -c "runner command here" output_file and it saved all the output as was desired. This was on Ubuntu 14.04 LTS
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.