help with debugging daemons on debian

I'm trying to get a daemon to work on debian using daemons gem. I
installed daemons
via gems and I'm running version 1.0.3.

I used daemon_generator from

I created a daemon called test and ran it on OSX with no problem,
meaning that

./script/daemons start
./script/daemons stop

both work fine.

I moved it to a debian system, modifed test.rb to use the production
environment, and I can't get it to work. I've tried a bunch of things.
doesn't seem to start reliably

On the debian system
./script/daemons start

sometimes works. I see the daemon running, I see the monitor daemon
running, and
messages are going into the production log file.

If I try to stop it with

./script/daemons stop

the test daemon stops but the monitor does *not* stop. Then I get a log
file for
the test daemon with

# Logfile created on Thu Dec 07 15:33:33 -0800 2006 by
failed to allocate memory
stack level too deep
exception reentered
uninitialized constant Rails
no such file to load -- rails.rb
No such file or directory - [snip]/log/
no such file to load -- openssl
no such file to load -- http-access2
no such file to load -- xmlscan/scanner
no such file to load -- xml/parser
no such file to load -- tmail/
no such file to load -- tmail/
test.rb.log (END)

Here is lib/daemons/test.rb:
#!/usr/bin/env ruby

#You might want to change this
ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/environment"

$running = true;
Signal.trap("TERM") do
  $running = false

while($running) do

  # Replace this with your code
  ActiveRecord::Base.logger << "This daemon is still running at

  sleep 10

and here is the ctl file:

#!/usr/bin/env ruby
require 'rubygems'
require "daemons"
require 'yaml'
require 'erb'
require 'active_support'

options = YAML.load(
  File.dirname(__FILE__) + "/../../config/daemons.yml"
options[:dir_mode] = options[:dir_mode].to_sym File.dirname(__FILE__) + '/test.rb', options
# this is a comment

Thank you