def daemonize(opts={}, &block)
      self.options = opts
      parse_options
      return unless ok_to_start?

      puts "Starting #{script_name}..."
      puts "Logging to: #{log_file}" unless ontop?
      
      unless ontop?
        safefork do
          open(pid_file, 'w'){|f| f << Process.pid }
          at_exit { remove_pid! }
    
          trap('TERM') { callback!(:sig_term)                            }
          trap('INT')  { callback!(:sig_int)  ; Process.kill('TERM', $$) }
          trap('HUP')  { callback!(:sig_hup)                             }

          sess_id = Process.setsid
          reopen_filehandes

          begin
            at_exit { callback!(:after) }
            callback!(:before)
            run_block(&block)
          rescue SystemExit
          rescue Exception => e
            $stdout.puts "Something bad happened #{e.inspect} #{e.backtrace.join("\n")}"
          end            
        end
      else
        begin
          callback!(:before)
          run_block(&block)
        rescue SystemExit, Interrupt
          callback!(:after)
        end
      end
    end
def daemonize(opts={}, &block)
      self.options = opts
      parse_options
      return unless ok_to_start?

      puts "Starting #{script_name}..."
      puts "Logging to: #{log_file}" unless ontop?
      
      unless ontop?
        safefork do
          open(pid_file, 'w'){|f| f << Process.pid }
          at_exit { remove_pid! }
    
          trap('TERM') { callback!(:sig_term)                            }
          trap('INT')  { callback!(:sig_int)  ; Process.kill('TERM', $$) }
          trap('HUP')  { callback!(:sig_hup)                             }

          sess_id = Process.setsid
          reopen_filehandes

          begin
            at_exit { callback!(:after) }
            callback!(:before)
            run_block(&block)
          rescue SystemExit
          rescue Exception => e
            $stdout.puts "Something bad happened #{e.inspect} #{e.backtrace.join("\n")}"
          end            
        end
      else
        begin
          callback!(:before)
          run_block(&block)
        rescue SystemExit, Interrupt
          callback!(:after)
        end
      end
    end
options may include:

 <tt>:loop_every</tt> Fixnum (DEFAULT 0)
  How many seconds to sleep between calls to your block

 <tt>:timeout</tt> Fixnum (DEFAULT 0)
  Timeout in if block does not execute withing passed number of seconds

 <tt>:kill_timeout</tt> Fixnum (DEFAULT 120)
  Wait number of seconds before using kill -9 on daemon

 <tt>:die_on_timeout</tt> BOOL (DEFAULT False)
  Should the daemon continue running if a block times out, or just run the block again

 <tt>:ontop</tt> BOOL (DEFAULT False)
  Do not daemonize.  Run in current process

 <tt>:before</tt> BLOCK
  Run this block after daemonizing but before begining the daemonize loop.
  You can also define the before block by putting a before do/end block in your class.

 <tt>:after</tt> BLOCK
  Run this block before program exists.
  You can also define the after block by putting an after do/end block in your class.

 <tt>:die_if</tt> BLOCK
  Run this check after each iteration of the loop.   If the block returns true, throw a DieTime exception and exit
  You can also define the after block by putting an die_if do/end block in your class.

 <tt>:exit_if</tt> BLOCK
  Run this check after each iteration of the loop.   If the block returns true, exit gracefully
  You can also define the after block by putting an exit_if do/end block in your class.

 <tt>:log_prefix</tt> BOOL (DEFAULT true)
  Prefix log file entries with PID and timestamp