Basically, you can pass an option hash as an argument to the method.
It allow some options to be adjusted:
* :umask: set the umask to be uses, default 0000
* :stdin: change the standard input, defaults to /dev/null
* :stdout: change the standard output, defaults do /dev/null
* :stderr: change the standard error output, defaults to stdout
* :pid_file: write the pid to the given file, you need to give an absolute path, otherwise it will write to "/"
* :rescue: a proc object or a method name as symbol to be called if a error is raised during the daemonizing process.
My english is broken, so it'd be nice if anybody could revise the documentation.
Rodrigo Kochenburger
[EMAIL PROTECTED]
Index: activesupport/lib/active_support/core_ext/kernel/daemonizing.rb
===================================================================
--- activesupport/lib/active_support/core_ext/kernel/daemonizing.rb (revision 3796)
+++ activesupport/lib/active_support/core_ext/kernel/daemonizing.rb (working copy)
@@ -1,15 +1,60 @@
module Kernel
# Turns the current script into a daemon process that detaches from the console.
# It can be shut down with a TERM signal.
- def daemonize
- exit if fork # Parent exits, child continues.
- Process.setsid # Become session leader.
- exit if fork # Zap session leader. See [1].
- Dir.chdir "/" # Release old working directory.
- File.umask 0000 # Ensure sensible umask. Adjust as needed.
- STDIN.reopen "/dev/null" # Free file descriptors and
- STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
- STDERR.reopen STDOUT # STDOUT/ERR should better go to a logfile.
- trap("TERM") { exit }
+ #
+ # It allow some options to be setted:
+ # <tt>umask</tt>: set the umask to be uses, default 0000
+ # <tt>stdin</tt>: change the standard input, defaults to /dev/null
+ # <tt>stdout</tt>: change the standard output, defaults do /dev/null
+ # <tt>stderr</tt>: change the standard error output, defaults to stdout
+ # <tt>pid_file</tt>: write the pid to the given file, you need to give an absolute path,
+ # otherwise it will write to /
+ # <tt>rescue</tt>: a proc object or a method name as symbol to be called if a error is raised
+ # during the daemonizing process.
+ #
+ # Note: It will not rescue errors raised in your code, only in the daemonize process,
+ # also if an error occur while setting stdin, stdout or stderr the block won't be called
+ # and errors will be write to stderr.
+ DEFAULT_DAEMON_OPTIONS = {
+ :umask => 0000,
+ :stdin => "/dev/null",
+ :stdout => "/dev/null",
+ :stderr => STDOUT,
+ :pid_file => nil
+ :rescue => nil
+ }.freeze
+
+ def daemonize(opts={})
+ opts = DEFAULT_DAEMON_OPTIONS.merge(opts)
+
+ begin
+ exit if fork # Parent exits, child continues.
+ Process.setsid # Become session leader.
+ exit if fork # Zap session leader. See [1].
+
+ pid = Process.pid
+ File.open(opts[:pid_file], "w") { |f| f.write(pid) } unless opts[:pid_file].nil?
+
+ Dir.chdir "/" # Release old working directory.
+ File.umask opts[:umask] # Ensure sensible umask. Adjust as needed.
+
+ STDIN.reopen opts[:stdin] # Free file descriptors and
+ STDOUT.reopen opts[:stdout], "a" # point them somewhere sensible.
+ STDERR.reopen opts[:stderr] # STDOUT/ERR should better go to a logfile.
+
+ trap("TERM") { exit }
+
+ pid
+ rescue => e
+ case opts[:rescue]
+ when Symbol
+ send(opts[:rescue], e)
+ when Proc
+ opts[:rescue].call(e)
+ else
+ raise
+ end
+ end
end
-end
\ No newline at end of file
+
+end
_______________________________________________ Rails-core mailing list [email protected] http://lists.rubyonrails.org/mailman/listinfo/rails-core
