Rails script/runner + logging + cron + production

May 26, 2009 by Andre

A quick tip: you're using script/runner in production (likely invoked via cron for a periodic background task), you probably are not seeing logging output in your production.log. Why? In production mode, the Rails logger doesn't auto-flush. As far as I can tell, the logging output is simply lost.

An easy solution that worked for me: adding a Rails.logger.flush to my script/runner call in cron.

Before:

*/1 * * * * deploy PATH_TO_APP/script/runner -e production 'MyClass.do_it'

After:

*/1 * * * * deploy PATH_TO_APP/script/runner -e production 'MyClass.do_it; Rails.logger.flush'

Bonus tip: to capture any other output from the cron task, use this:

*/1 * * * * deploy PATH_TO_APP/script/runner -e production 'MyClass.do_it; Rails.logger.flush' >> PATH_TO_APP/log/my.log 2>&1

Happy logging!

Comments

1

Andy Stewart on Sep 10

Another way to capture all cron output, including errors, is to use my fyi gem like this:

*/1 * * * * fyi "deploy PATH_TO_APP/script/runner -e production 'MyClass.do_it'"

In fact you probably don't even need the double-quotation marks.

Post a comment

 
This is so filters can reject the spam-bots. Thanks!