DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Daemonize A Ruby Process

12.15.2011
| 4463 views |
  • submit to reddit
        Neat separation of responsibilities between fork/process stuff and actual app 

  #!/usr/bin/ruby

  daemonize do
    worker = Resque::Worker.new(*queues)
    worker.work
  end

  def daemonize &block
    child = fork
    if child.nil? # is child
      $stdout.close
      $stdout = open("/dev/null")
      $stdin.close
      trap('HUP', 'IGNORE')
      block.call
    else # is parent
      Process.detach child
    end
  end