| Module | Daemonize |
| In: |
lib/dctl/daemonize.rb
lib/dctl/pidfile.rb |
February. 4, 2005 Travis Whitton <whitton@atlantic.net>
Daemonize allows you to easily modify any existing Ruby program to run as a daemon. See README.rdoc for more details.
build the docs if you want to
The Daemonize extension module is copywrited free software by Travis Whitton <whitton@atlantic.net>. You can redistribute it under the terms specified in the COPYING file of the Ruby distribution.
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Daemonize is a module derived from Perl’s Proc::Daemon module. This module allows you to easily modify any existing Ruby program to run as a daemon. A daemon is a process that runs in the background with no controlling terminal. Generally servers (like FTP and HTTP servers) run as daemon processes. Note, do not make the mistake that a daemon == server. Converting a program to a daemon by hand is a relatively simple process; however, this module will save you the effort of repeatedly looking up the procedure, and it will also insure that your programs are daemonized in the safest and most corrects fashion possible.
The Daemonize module does the following:
Forks a child and exits the parent process.
Becomes a session leader (which detaches the program from the controlling terminal).
Forks another child process and exits first child. This prevents the potential of acquiring a controlling terminal.
Changes the current working directory to "/".
Clears the file creation mask.
Closes file descriptors.
Using the Daemonize module is extremely simple:
require 'daemonize'
class TestDaemon
include Daemonize
def initialize
daemonize()
loop do
# do some work here
end
end
end
Daemonize was written by Travis Whitton and is based on Perl’s Proc::Daemonize, which was written by Earl Hood. The above documentation is also partially borrowed from the Proc::Daemonize POD documentation.
| VERSION | = | "0.1.1n" |
This method causes the current running process to become a daemon
# File lib/dctl/daemonize.rb, line 107
107: def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null', oldmode=true)
108: stdin = File.expand_path stdin
109: stdout = File.expand_path stdout
110: stderr = File.expand_path stderr
111:
112: srand # Split rand streams between spawning and daemonized process
113: safefork and exit # Fork and exit from the parent
114:
115: # Detach from the controlling terminal
116: unless sess_id = Process.setsid
117: raise SystemCallError, 'cannot detach from controlled terminal'
118: end
119:
120: # Prevent the possibility of acquiring a controlling terminal
121: if oldmode
122: trap 'SIGHUP', 'IGNORE'
123: safefork and exit
124: end
125:
126: Dir.chdir "/" # Release old working directory
127: File.umask 0000 # Insure sensible umask
128:
129: # Make sure all file descriptors are closed
130: ObjectSpace.each_object(IO) do |io|
131: unless [STDIN, STDOUT, STDERR].include?(io)
132: io.close rescue nil
133: end
134: end
135:
136: STDIN.reopen stdin # Free file descriptors and
137: STDOUT.reopen stdout, "a" # point them somewhere sensible
138: STDERR.reopen stderr, "a" # STDOUT/STDERR should go to a logfile
139: sess_id # Return value is mostly irrelevant
140: end