<?xml version="1.0" encoding="UTF-8"?>
<snippet>
  <account-id type="integer">2</account-id>
  <body>This snippets shows you how to create a daemon process out of an ordinary Ruby script.

First you'll need the daemons gem:

&lt;code&gt;
gem install daemons
&lt;/code&gt;

Then you'll need the daemon script, for example daemon.rb:

&lt;code&gt;
require 'rubygems'
require 'daemons'

pwd  = File.dirname(File.expand_path(__FILE__))
file = pwd + '/../lib/background_service.rb'

Daemons.run_proc(
  'background_service', # name of daemon
#  :dir_mode =&gt; :normal
#  :dir =&gt; File.join(pwd, 'tmp/pids'), # directory where pid file will be stored
#  :backtrace =&gt; true,
#  :monitor =&gt; true,
  :log_output =&gt; true
) do
  exec &quot;ruby #{file}&quot;
end
&lt;/code&gt;

Change the file variable to point to the script you want to daemonize and your good to go.

You can now execute the daemon.rb script without parameters to get a list of available commands for controlling the daemon process:

&lt;code&gt;
ERROR: no command given

Usage: lib/background_service.rb &lt;command&gt; &lt;options&gt; -- &lt;application options&gt;

* where &lt;command&gt; is one of:
  start         start an instance of the application
  stop          stop all instances of the application
  restart       stop all instances and restart them afterwards
  run           start the application and stay on top
  zap           set the application to a stopped state

* and where &lt;options&gt; may contain several of the following:

    -t, --ontop                      Stay on top (does not daemonize)
    -f, --force                      Force operation

Common options:
    -h, --help                       Show this message
        --version                    Show version
&lt;/code&gt;

</body>
  <comments-count type="integer">0</comments-count>
  <created-at type="datetime">2008-06-27T18:46:43+03:00</created-at>
  <id type="integer">212</id>
  <language-id type="integer">124</language-id>
  <rendered-body>&lt;p&gt;This snippets shows you how to create a daemon process out of an ordinary Ruby script.&lt;/p&gt;


	&lt;p&gt;First you&amp;#8217;ll need the daemons gem:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; gem install daemons
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;Then you&amp;#8217;ll need the daemon script, for example daemon.rb:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;rubygems&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;daemons&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt; pwd  &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;LibraryClassType&quot;&gt;File&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;dirname&lt;/span&gt;(&lt;span class=&quot;LibraryClassType&quot;&gt;File&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;expand_path&lt;/span&gt;(&lt;span class=&quot;Variable&quot;&gt;__FILE__&lt;/span&gt;))
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt; file &lt;span class=&quot;Operator&quot;&gt;=&lt;/span&gt; pwd &lt;span class=&quot;Operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;/../lib/background_service.rb&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt; &lt;span class=&quot;LibraryClassType&quot;&gt;Daemons&lt;/span&gt;.&lt;span class=&quot;FunctionName&quot;&gt;run_proc&lt;/span&gt;(
&lt;span class=&quot;line-numbers&quot;&gt;   8 &lt;/span&gt;   &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;background_service&lt;span class=&quot;String&quot;&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt; name of daemon&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   9 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt;  :dir_mode =&amp;gt; :normal&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  10 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt;  :dir =&amp;gt; File.join(pwd, 'tmp/pids'), # directory where pid file will be stored&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  11 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt;  :backtrace =&amp;gt; true,&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  12 &lt;/span&gt; &lt;span class=&quot;LineComment&quot;&gt;&lt;span class=&quot;LineComment&quot;&gt;#&lt;/span&gt;  :monitor =&amp;gt; true,&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  13 &lt;/span&gt;   &lt;span class=&quot;UserDefinedConstant&quot;&gt;&lt;span class=&quot;UserDefinedConstant&quot;&gt;:&lt;/span&gt;log_output&lt;/span&gt; =&amp;gt; &lt;span class=&quot;BuiltInConstant&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  14 &lt;/span&gt; ) &lt;span class=&quot;Keyword&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  15 &lt;/span&gt;   exec &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;ruby &lt;span class=&quot;String&quot;&gt;&lt;span class=&quot;String&quot;&gt;#{&lt;/span&gt;file&lt;span class=&quot;String&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;String&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;  16 &lt;/span&gt; &lt;span class=&quot;Keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;Change the file variable to point to the script you want to daemonize and your good to go.&lt;/p&gt;


	&lt;p&gt;You can now execute the daemon.rb script without parameters to get a list of available commands for controlling the daemon process:&lt;/p&gt;


	&lt;p&gt;&lt;pre class=&quot;active4d&quot;&gt;&lt;span class=&quot;line-numbers&quot;&gt;   1 &lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;ERROR&lt;/span&gt;: no command given
&lt;span class=&quot;line-numbers&quot;&gt;   2 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   3 &lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;Usage&lt;/span&gt;: lib&lt;span class=&quot;Operator&quot;&gt;/&lt;/span&gt;background_service.&lt;span class=&quot;FunctionName&quot;&gt;rb&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;&amp;lt;&lt;/span&gt;command&lt;span class=&quot;Operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;&amp;lt;&lt;/span&gt;options&lt;span class=&quot;Operator&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;&amp;lt;&lt;/span&gt;application options&lt;span class=&quot;Operator&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;line-numbers&quot;&gt;   4 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;   5 &lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;*&lt;/span&gt; where &lt;span class=&quot;Operator&quot;&gt;&amp;lt;&lt;/span&gt;command&lt;span class=&quot;Operator&quot;&gt;&amp;gt;&lt;/span&gt; is one of:
&lt;span class=&quot;line-numbers&quot;&gt;   6 &lt;/span&gt;   start         start an instance of the application
&lt;span class=&quot;line-numbers&quot;&gt;   7 &lt;/span&gt;   stop          stop all instances of the application
&lt;span class=&quot;line-numbers&quot;&gt;   8 &lt;/span&gt;   restart       stop all instances &lt;span class=&quot;Operator&quot;&gt;and&lt;/span&gt; restart them afterwards
&lt;span class=&quot;line-numbers&quot;&gt;   9 &lt;/span&gt;   run           start the application &lt;span class=&quot;Operator&quot;&gt;and&lt;/span&gt; stay on top
&lt;span class=&quot;line-numbers&quot;&gt;  10 &lt;/span&gt;   zap           set the application to a stopped state
&lt;span class=&quot;line-numbers&quot;&gt;  11 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  12 &lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;Operator&quot;&gt;and&lt;/span&gt; where &lt;span class=&quot;Operator&quot;&gt;&amp;lt;&lt;/span&gt;options&lt;span class=&quot;Operator&quot;&gt;&amp;gt;&lt;/span&gt; may contain several of the following:
&lt;span class=&quot;line-numbers&quot;&gt;  13 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  14 &lt;/span&gt;     &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;t, &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;ontop                      &lt;span class=&quot;Variable&quot;&gt;Stay&lt;/span&gt; on top (does &lt;span class=&quot;Operator&quot;&gt;not&lt;/span&gt; daemonize)
&lt;span class=&quot;line-numbers&quot;&gt;  15 &lt;/span&gt;     &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;f, &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;force                      &lt;span class=&quot;Variable&quot;&gt;Force&lt;/span&gt; operation
&lt;span class=&quot;line-numbers&quot;&gt;  16 &lt;/span&gt; 
&lt;span class=&quot;line-numbers&quot;&gt;  17 &lt;/span&gt; &lt;span class=&quot;Variable&quot;&gt;Common&lt;/span&gt; options:
&lt;span class=&quot;line-numbers&quot;&gt;  18 &lt;/span&gt;     &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;h, &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;help                       &lt;span class=&quot;Variable&quot;&gt;Show&lt;/span&gt; this message
&lt;span class=&quot;line-numbers&quot;&gt;  19 &lt;/span&gt;         &lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;Operator&quot;&gt;-&lt;/span&gt;version                    &lt;span class=&quot;Variable&quot;&gt;Show&lt;/span&gt; version
&lt;/pre&gt;&lt;/p&gt;</rendered-body>
  <title>How to create a daemon process using Ruby and the daemons RubyGem</title>
  <updated-at type="datetime">2008-10-25T22:52:22+03:00</updated-at>
</snippet>
