From: Gregory Brown Date: 2006-12-13T23:45:40+09:00 Subject: Re: How do I run WEBrick as a daemon? On 12/13/06, Ashley Moran wrote: > The skeleton of the code is below. I've noticed there is a > WEBrick::Daemon class but I can't figure out how to use it. I've > tried using script/server from Rails as a template but it's pretty > complex. Can anyone offer some hints? *blows dust off of gambit* We just pass a become_daemon flag in our subclass of Webrick::HTTPServer, and then use this to determine whether or not to start the Daemon. There is some extra stuff in there, but I figured it'd be useful to see it in context. def initialize( game_class, port = 80, html_dir = "html", become_daemon = false ) super(:Port => port) @html_dir = File.join(File.expand_path(File.dirname($0)), html_dir) self.class.const_set(:SERVER, self) @server_message = nil @game_class = game_class @games = Hash.new @players = Hash.new WEBrick::Daemon.start if become_daemon mount_proc("/") { |req, resp| serve_file(req, resp, req.path) } mount_proc("/view") { |req, resp| serve_view(req, resp) } mount_proc("/event") { |req, resp| serve_event(req, resp) } mount_proc("/server") { |req, resp| serve_server_event(req, resp) } ['INT', 'TERM'].each { |signal| trap(signal) { shutdown } } end