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 Answer
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')
6 Comments
nickw444
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)
nickw444
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.
nickw444
Just looked at your link, but it doesn't seem to be making these files that a specify.
Dave Newton
@nickw444 Because puts writes to stdout.
Jignesh Gohel
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 |