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
http://kylemaxwell.typepad.com/everystudent/2006/08/after_writing_r.html

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

./script/daemons start
and
./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.
It
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
logger.rb/1.5.2.9
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/test.rb.pid
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/scanner_c.so
no such file to load -- tmail/base64.so
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
end

while($running) do

  # Replace this with your code
  ActiveRecord::Base.logger << "This daemon is still running at
#{Time.now}.\n"

  sleep 10
end

and here is the ctl file:

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

options = YAML.load(
  ERB.new(
  IO.read(
  File.dirname(__FILE__) + "/../../config/daemons.yml"
  )).result).with_indifferent_access
options[:dir_mode] = options[:dir_mode].to_sym

Daemons.run File.dirname(__FILE__) + '/test.rb', options
# this is a comment

Thank you