public Method

Daemons.run(script, options = {})

Contents:

Passes control to Daemons. This is used in wrapper-scripts that are supposed to control other ruby scripts or external applications. Control is completely passed to the daemons library. Such wrapper script should be invoked with command line options like ‘start’ or ‘stop’ to do anything useful.

script:This is the path to the script that should be run as a daemon. Please note that Daemons runs this script with load <script>. Also note that Daemons cannot detect the directory in which the controlling script resides, so this has to be either an absolute path or you have to run the controlling script from the appropriate directory.
options:A hash that may contain one or more of the options listed below

Options:

:app_name:The name of the application. This will be used to contruct the name of the pid files and log files. Defaults to the basename of the script.
:ARGV:An array of strings containing parameters and switches for Daemons. If not given, ARGV (the parameters given to the Ruby process) will be used.
:dir_mode:Either :script (the directory for writing the pid files to given by :dir is interpreted relative to the script location given by script) or :normal (the directory given by :dir is interpreted as a (absolute or relative) path) or :system (/var/run is used as the pid file directory)
:dir:Used in combination with :dir_mode (description above)
:multiple:Specifies whether multiple instances of the same script are allowed to run at the same time
:ontop:When given (i.e. set to true), stay on top, i.e. do not daemonize the application (but the pid-file and other things are written as usual)
:mode::load Load the script with Kernel.load; :exec Execute the script file with Kernel.exec
:backtrace:Write a backtrace of the last exceptions to the file ’[app_name].log’ in the pid-file directory if the application exits due to an uncaught exception
:monitor:Monitor the programs and restart crashed instances
:log_output:When given (i.e. set to true), redirect both STDOUT and STDERR to a logfile
named '[app_name].output' in the pid-file directory

Example:

options = {
  :app_name   => "my_app",
  :ARGV       => ['start', '-f']
  :dir_mode   => :script,
  :dir        => 'pids',
  :multiple   => true,
  :ontop      => true,
  :mode       => :exec,
  :backtrace  => true,
  :monitor    => true,
  :script     => "path/to/script.rb"
}

Daemons.run(File.join(File.dirname(__FILE__), 'myscript.rb'), options)</script></tt>

Source Code

# File daemons.rb, line 131
def run(script, options = {})
  options[:script] = script
  @controller = Controller.new(options, options[:ARGV] || ARGV)

  @controller.catch_exceptions {
    @controller.run
  }

  # I don't think anybody will ever use @group, as this location should not be reached under non-error conditions
  @group = @controller.group
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.