All functions and classes that Daemons provides reside in this module.
Daemons is normally invoked by one of the following four ways:
or: | why do my daemons crash when they try to open a file? |
or: | why can I not see any output from the daemon on the console (when using for example puts? |
From a technical aspect of view, daemons does the following when creating a daemon:
So what does this mean for your daemons:
Also, you are maybe interested in reading the documentation for the class PidFile. There you can find out about how Daemons works internally and how and where the so called PidFiles are stored.
VERSION | = | "1.0.3" |
Execute the block in a new daemon. Daemons.call will return immediately after spawning the daemon with the new Application object as a return value.
options: | A hash that may contain one or more of the options listed below |
block: | The block to call in the daemon. |
:multiple: | Specifies whether multiple instances of the same script are allowed to run at the same time |
:ontop: | When given, stay on top, i.e. do not daemonize the application |
: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 |
options = { :backtrace => true, :monitor => true, :ontop => true } Daemons.call(options) begin # Server loop: loop { conn = accept_conn() serve(conn) } end
Daemonize the currently runnig process, i.e. the calling process will become a daemon.
options: | A hash that may contain one or more of the options listed below |
:ontop: | When given, stay on top, i.e. do not daemonize the application |
: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 |
options = { :backtrace => true, :ontop => true } Daemons.daemonize(options) # Server loop: loop { conn = accept_conn() serve(conn) }
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 |
: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. |
: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, 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 |
options = { :app_name => "my_app", :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)
Passes control to Daemons. This function does the same as Daemons.run except that not a script but a proc will be run as a daemon while this script provides command line options like ‘start’ or ‘stop’ and the whole pid-file management to control the proc.
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. |
options: | A hash that may contain one or more of the options listed in the documentation for Daemons.run |
A block must be given to this function. The block will be used as the :proc entry in the options hash.
Daemons.run_proc('myproc.rb') do loop do accept_connection() read_request() send_response() close_connection() end end