Yesterday I've encountered a sudden bug: my four years old script rafused to work. After a slight rewrite I got some kind of descriptive symptom in the syslog:
Fatal: Bad file descriptor
/usr/local/lib/ruby/gems/1.8/gems/deprecated-2.0.1/lib/deprecated.rb:199:in `write'
/usr/local/lib/ruby/gems/1.8/gems/deprecated-2.0.1/lib/deprecated.rb:199
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
/usr/local/lib/ruby/site_ruby/1.8/dbi.rb:48
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require' /usr/work/flow-tools/flow-pg_import:13
deprecated.rb obviously doesn't have any mention of 'write' so I've suspected an issue with some sudden output; it appeared to be true: deprecated.rb had a repeated declaration of a constant on line 199 which caused an error message on stderr which was unavailable (the script was exec'ed as a helper by another program). So I did just this:
$stdout.reopen("/dev/null", "w")
$stderr.reopen("/dev/null", "w")
And it cured the script. Rationale: you should always redirect the standard I/O streams when you really mean it to work in background.