Ruby Daemons

rdoc-image:https://codeclimate.com/github/acuppy/daemons/badges/gpa.svg[https://codeclimate.com/github/acuppy/daemons]

" target="_parent" title=" <p>Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be <em>run as a daemon</em> and to be <em>controlled by simple start/stop/restart commands</em>.</p> <p>If you want, you can also use daemons to <em>run blocks of ruby code in a daemon process</em> and to control these processes from the main application.</p> <p>Besides this basic functionality, daemons offers many advanced features like <em>exception backtracing</em> and logging (in case your ruby script crashes) and <em>monitoring</em> and automatic restarting of your processes if they crash.</p> <h2 id="label-Basic+Usage">Basic Usage</h2> <p>You can use Daemons in four different ways:</p> <h3 id="label-1.+Create+wrapper+scripts+for+your+server+scripts+or+applications">1. Create wrapper scripts for your server scripts or applications</h3> <p>Layout: suppose you have your self-written server <code>myserver.rb</code>:</p> <pre><code># this is myserver.rb # it does nothing really useful at the moment loop do sleep(5) end </code></pre> <p>To use <code>myserver.rb</code> in a production environment, you need to be able to run <code>myserver.rb</code> in the <em>background</em> (this means detach it from the console, fork it in the background, release all directories and file descriptors).</p> <p>Just create <code>myserver_control.rb</code> like this:</p> <pre><code># this is myserver_control.rb require &#39;daemons&#39; Daemons.run(&#39;myserver.rb&#39;) </code></pre> <p>And use it like this from the console:</p> <pre><code>$ ruby myserver_control.rb start (myserver.rb is now running in the background) $ ruby myserver_control.rb restart (...) $ ruby myserver_control.rb stop </code></pre> <p>For testing purposes you can even run <code>myserver.rb</code> <em>without forking</em> in the background:</p> <pre><code>$ ruby myserver_control.rb run </code></pre> <p>An additional nice feature of Daemons is that you can pass <em>additional arguments</em> to the script that should be daemonized by seperating them by two <em>hyphens</em>:</p> <pre><code>$ ruby myserver_control.rb start -- --file=anyfile --a_switch another_argument </code></pre> <h3 id="label-2.+Create+wrapper+scripts+that+include+your+server+procs">2. Create wrapper scripts that include your server procs</h3> <p>Layout: suppose you have some code you want to run in the background and control that background process from a script:</p> <pre><code># this is your code # it does nothing really useful at the moment loop do sleep(5) end </code></pre> <p>To run this code as a daemon create <code>myproc_control.rb</code> like this and include your code:</p> <pre><code># this is myproc_control.rb require &#39;daemons&#39; Daemons.run_proc(&#39;myproc.rb&#39;) do loop do sleep(5) end end </code></pre> <p>And use it like this from the console:</p> <pre><code>$ ruby myproc_control.rb start (myproc.rb is now running in the background) $ ruby myproc_control.rb restart (...) $ ruby myproc_control.rb stop </code></pre> <p>For testing purposes you can even run <code>myproc.rb</code> <em>without forking</em> in the background:</p> <pre><code>$ ruby myproc_control.rb run </code></pre> <h3 id="label-3.+Control+a+bunch+of+daemons+from+another+application">3. Control a bunch of daemons from another application</h3> <p>Layout: you have an application <code>my_app.rb</code> that wants to run a bunch of server tasks as daemon processes.</p> <pre><code># this is my_app.rb require &#39;daemons&#39; task1 = Daemons.call(:multiple =&gt; true) do # first server task loop do conn = accept_conn() serve(conn) end end task2 = Daemons.call do # second server task loop do something_different() end end # the parent process continues to run # we can even control our tasks, for example stop them task1.stop task2.stop exit </code></pre> <h3 id="label-4.+Daemonize+the+currently+running+process">4. Daemonize the currently running process</h3> <p>Layout: you have an application <code>my_daemon.rb</code> that wants to run as a daemon (but without the ability to be controlled by daemons via start/stop commands)</p> <pre><code># this is my_daemons.rb require &#39;daemons&#39; # Initialize the app while we&#39;re not a daemon init() # Become a daemon Daemons.daemonize # The server loop loop do conn = accept_conn() serve(conn) end </code></pre> <p>For further documentation, refer to the module documentation of Daemons.</p> <h2 id="label-Displaying+daemon+status">Displaying daemon status</h2> <p>When daemonizing a process using a wrapper script, as examples 1 and 2 above, the status can be shown using</p> <pre><code>$ ruby myproc_control.rb status </code></pre> <p>By default this will display whether or not the daemon is running and, if it is, its PID.</p> <p>A custom message can be shown with</p> <pre><code>def custom_show_status(app) # Display the default status information app.default_show_status puts puts &quot;PS information&quot; system(&quot;ps -p \#{app.pid.pid.to_s">

Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be run as a daemon and to be controlled by simple start/stop/restart commands.

If you want, you can also use daemons to run blocks of ruby code in a daemon process and to control these processes from the main application.

Besides this basic functionality, daemons offers many advanced features like exception backtracing and logging (in case your ruby script crashes) and monitoring and automatic restarting of your processes if they crash.

Basic Usage

You can use Daemons in four different ways:

1. Create wrapper scripts for your server scripts or applications

Layout: suppose you have your self-written server myserver.rb:

# this is myserver.rb # it does nothing really useful at the moment  loop do   sleep(5) end 

To use myserver.rb in a production environment, you need to be able to run myserver.rb in the background (this means detach it from the console, fork it in the background, release all directories and file descriptors).

Just create myserver_control.rb like this:

# this is myserver_control.rb require 'daemons'  Daemons.run('myserver.rb') 

And use it like this from the console:

$ ruby myserver_control.rb start     (myserver.rb is now running in the background) $ ruby myserver_control.rb restart     (...) $ ruby myserver_control.rb stop 

For testing purposes you can even run myserver.rb without forking in the background:

$ ruby myserver_control.rb run 

An additional nice feature of Daemons is that you can pass additional arguments to the script that should be daemonized by seperating them by two hyphens:

$ ruby myserver_control.rb start -- --file=anyfile --a_switch another_argument 

2. Create wrapper scripts that include your server procs

Layout: suppose you have some code you want to run in the background and control that background process from a script:

# this is your code # it does nothing really useful at the moment  loop do   sleep(5) end 

To run this code as a daemon create myproc_control.rb like this and include your code:

# this is myproc_control.rb require 'daemons'  Daemons.run_proc('myproc.rb') do   loop do     sleep(5)   end end 

And use it like this from the console:

$ ruby myproc_control.rb start     (myproc.rb is now running in the background) $ ruby myproc_control.rb restart     (...) $ ruby myproc_control.rb stop 

For testing purposes you can even run myproc.rb without forking in the background:

$ ruby myproc_control.rb run 

3. Control a bunch of daemons from another application

Layout: you have an application my_app.rb that wants to run a bunch of server tasks as daemon processes.

# this is my_app.rb require 'daemons'  task1 = Daemons.call(:multiple => true) do   # first server task    loop do     conn = accept_conn()     serve(conn)   end end  task2 = Daemons.call do   # second server task    loop do     something_different()   end end  # the parent process continues to run  # we can even control our tasks, for example stop them task1.stop task2.stop  exit 

4. Daemonize the currently running process

Layout: you have an application my_daemon.rb that wants to run as a daemon (but without the ability to be controlled by daemons via start/stop commands)

# this is my_daemons.rb require 'daemons'  # Initialize the app while we're not a daemon init()  # Become a daemon Daemons.daemonize  # The server loop loop do   conn = accept_conn()   serve(conn) end 

For further documentation, refer to the module documentation of Daemons.

Displaying daemon status

When daemonizing a process using a wrapper script, as examples 1 and 2 above, the status can be shown using

$ ruby myproc_control.rb status 

By default this will display whether or not the daemon is running and, if it is, its PID.

A custom message can be shown with

def custom_show_status(app)   # Display the default status information   app.default_show_status    puts   puts "PS information"   system("ps -p #{app.pid.pid.to_s</a>")

  puts
  puts "Size of log files"
  system("du -hs /path/to/logs")
end

Daemons.run('myserver.rb', { show_status_callback: :custom_show_status })

Documentation

Documentation can be found at www.rubydoc.info/gems/daemons.

Author

Written 2005-2021 by Thomas Uehlinger, 2014-2016 by Aaron Stone.