1

I have a ruby script that writes to STDOUT and sometimes STDERR. I am running this script every 5 minutes via a crontab:

*/5 * * * * /root/myscript.rb >> /var/log/myscript.log 2>&1

What I've noticed is that the log file will not be written to in real time. Instead, it waits until the script has exited, and then all at once writes to that log file. I feel like I have other scripts that do not have this behavior, but perhaps I am mistaken.

Is this just how it works? Is there something I should look for to change this behavior?

This is Ubuntu 12.04

Brian
  • 240
  • 3
  • 16

2 Answers2

1

Have your script log to syslog instead of stdout. Doing so is a much more flexible solution in the long term than logging to stdout.

EEAA
  • 109,363
  • 18
  • 175
  • 245
  • Why do you say that? Sometimes I will execute this script manually, and it's nice to just have it write output directly to my console, instead of opening the syslog as well. – Brian Jul 25 '13 at 14:16
  • Syslog is the de facto logging standard on Linux. As such, tools for collecting, analyzing, and archiving syslog are numerous and of high quality. If you need console output, just modify your script to give verbose output if its run with a -v flag. – EEAA Jul 25 '13 at 14:21
1

Ah, I've found that Ruby will not automatically flush STDOUT/STDERR output after each write call. I placed STDOUT.sync = true at the top of my file and now it writes to STDOUT, and thus the log file, in real time.

Brian
  • 240
  • 3
  • 16