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?

sawa
  • 165,429
  • 45
  • 277
  • 381
nickw444
  • 948
  • 2
  • 9
  • 18
  • Do you want to do this within the Ruby application, or within the terminal that runs the Ruby application? – sawa Jan 03 '12 at 23:39

1 Answers1

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

clyfe
  • 23,695
  • 8
  • 85
  • 109
  • 1
    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 Jan 03 '12 at 23:38
  • 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 Jan 03 '12 at 23:42
  • Just looked at your link, but it doesn't seem to be making these files that a specify. – nickw444 Jan 03 '12 at 23:49
  • @nickw444 Because puts writes to stdout. – Dave Newton Jan 04 '12 at 00:06
  • `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 – Jignesh Gohel Apr 20 '18 at 15:33
  • I found reference to my posted solution at https://askubuntu.com/questions/290322/how-to-get-and-copy-a-too-long-output-completely-in-terminal#comment1668695_715798 – Jignesh Gohel Apr 20 '18 at 15:34