Unix redirect from long running script

I have a cron job that looks something like this:

*/3 * * * * ruby myscript.rb > /my/location/file.txt

      

myscript.rb looks something like this:

#!/usr/bin/env ruby
sleep(120)
puts "Hello"

      

So, every three minutes I run a script that runs for two minutes before writing to STDOUT. (Truth be told, myscript.rb is making a ton of HTTP requests and not sleeping. My suspicion is that this matters.)

The weird result is that for some percentage of the time /my/location/file.txt is empty.

The script seems to run, the file is emptied, and only when the script ends is there data written to the file.

This is not how * nix redirection works, right?

+3


source to share


2 answers


The ruby ​​process is probably just buffering its output to set STDOUT to always flush its output, set sync = true for STDOUT:



STDOUT.sync = true

      

+1


source


You are correct about file.txt

truncating as soon as the command is executed. If you need the old content of the file, unless the script completes the job, you will probably need to write another temporary one file-tmp.txt

and then rename it to file.txt

. If you just want to see the output ASAP, see @ kyle-burton's comment - you need to uncheck the checkbox in your script.



+1


source







All Articles