Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix redirection of a long-running script

I've got a cron job that essentially looks like this:

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

myscript.rb essentially looks like this:

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

So every three minutes I'm running a script that runs for two minutes before writing to STDOUT. (In truth, myscript.rb is making a ton of HTTP requests, not sleeping. My suspicion is that it's the time that matters.)

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

It seems as if the script starts, the file is emptied, and only when the script gets to the end is there data written to the file.

This isn't how *nix redirection works though, right?

like image 780
user225643 Avatar asked Nov 25 '25 07:11

user225643


1 Answers

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

STDOUT.sync = true
like image 91
Kyle Burton Avatar answered Nov 28 '25 01:11

Kyle Burton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!