--001a1133a8cc28713004f2d93e7f
Content-Type: text/plain; charset=ISO-8859-1

I'm in the same boat you are at the moment, learning the various ways of
daemonizing code. So far, I've gotten a couple methods working.

First, have you tried forking your code? Look at the method described here:
http://soa.dzone.com/snippets/daemonize-ruby-process

That was the first thing that I got working successfully. Of course you
have to manually kill the process afterward, using the 'kill' command in
the terminal, because there isn't an automated way. To make my life easier,
between the "pid  ork do" and the "loop do" I added this line: "puts
"\'script_name\' pid is set to #{Process.pid}"" so I can make a note of
what the pid is when I need to kill it later.

The second thing I got working was Daemons. Daemons' documentation is
easier to read than the documentation for some other daemon gems. With
Daemons, you have your code, let's suppose it is in 'file_checker.rb'.
Create a new file, 'file_checker_daemon.rb'. Then in
'file_checker_daemon.rb' put the following code, as explained in the
Daemons documentation at http://rubydoc.info/gems/daemons/1.1.9/frames:

require 'daemons'
Daemons.run_proc do
   loop do
      sleep(5)
   end
end

Afterwards, to daemonize your code simply do

ruby file_checker_daemon.rb start

and to stop it do

ruby file_checker_daemon.rb stop

Daemons will take care of remembering the pid, and the 'start' and 'stop'
methods make it super easy to control, unlike the manual process of forking
your code.

Hope that helps. Right now I'm trying to figure out how to do the same
thing using Dante and God. Maybe someone can help me? Dante seems to be for
webservers only, I can't seem to find in the documentation how to simply
daemonize some code. And for God, I have the following config, but it's not
working, I keep getting "start command exited with non-zero code  ".

filename is 'script.god'

God.watch do |w|
  w.name  script"
  w.dir  /Users/me/ruby/"
  w.pid_file  .pid_file  ile.join("log/script.pid")
  w.log  log/script.log"
  w.start  script.rb -P /log/script.pid  -d"
end

Any suggestions, what am I doing wrong??

Thanks!




On Thu, Feb 20, 2014 at 8:45 AM, cynic limbu <lists / ruby-forum.com> wrote:

> I'm having a problem using daemons (it's a gem used for running ruby
> scripts in the background)
>
> There are number of tutorials floating on the web but I seriously cannot
> understand.
>
> Can someone shorten it and explain it to me? Thank you.
>
> I'd like to run the following code in the background
>
>
> Dir.chdir ENV['HOME']
> if File.exists?("importantfile.rb")
> nil
> else
> newfile  ile.new("importantfile.rb", "w")
> newfile.print("something")
> newfile.close
> end
>
> --
> Posted via http://www.ruby-forum.com/.
>

--001a1133a8cc28713004f2d93e7f
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir="ltr">I&#39;m in the same boat you are at the moment, learning the various ways of daemonizing code. So far, I&#39;ve gotten a couple methods working.<div><br></div><div>First, have you tried forking your code? Look at the method described here:a href="http://soa.dzone.com/snippets/daemonize-ruby-process">http://soa.dzone.com/snippets/daemonize-ruby-process</a></div>
<div><br></div><div>That was the first thing that I got working successfully. Of course you have to manually kill the process afterward, using the &#39;kill&#39; command in the terminal, because there isn&#39;t an automated way. To make my life easier, between the &quot;pid = fork do&quot; and thequot;loop do&quot; I added this line: &quot;puts &quot;\&#39;script_name\&#39; pid is set to #{Process.pid}&quot;&quot; so I can make a note of whathe pid is when I need to kill it later./div>
<div><br></div><div>The second thing I got working was Daemons. Daemons&#39; documentation is easier to read than the documentation for some other daemon gems. With Daemons, you have your code, let&#39;s suppose it is in &#39;file_checker.rb&#39;. Create a new file, &#39;file_checker_daemon.rb&#39;.hen in &#39;file_checker_daemon.rb&#39; put the following code, as explained in the Daemons documentation at http://rubydoc.info/gems/daemons/1.1.9/frames:</div>
<div><br></div><div>require &#39;daemons&#39;</div><div>Daemons.run_proc do</div><div> O𨵩 伯伯򾼯    <div><br></div><div>ruby file_checker_daemon.rb start</div><div><br></div><div>and to stop it do</div><div><br></div><div>ruby file_checker_daemon.rb stop<br></div><div><br></div><div>Daemons will take care of remembering theid, and the &#39;start&#39; and &#39;stop&#39; methods make it super easyo control, unlike the manual process of forking your code./div>
<div><br></div><div>Hope that helps. Right now I&#39;m trying to figure outow to do the same thing using Dante and God. Maybe someone can help me? Dante seems to be for webservers only, I can&#39;t seem to find in the documentation how to simply daemonize some code. And for God, I have the following config, but it&#39;s not working, I keep getting &quot;start command exited with non-zero code = 1&quot;.</div>
<div><br></div><div>filename is &#39;script.god&#39;</div><div><br></div><div><div>God.watch do |w|</div><div>w.name3D &quot;script&quot;</div><div>w.dir = &quot;/Users/me/ruby/&quot;</div>
<div>w.pid_file = w.pid_file = File.join(&quot;log/script.pid&quot;)</div><div>w.log = &quot;log/script.log&quot;</div><div>w.start = &quot;script.rb -P /log/script.pid d&quot;</div><div>end</div></div><div><br></div>
<div>Any suggestions, what am I doing wrong??</div><div><br></div><div>Thanks!</div>







<div><br></div><div><br></div></div><div class="gmail_extra"><br><br><divlass="gmail_quote">On Thu, Feb 20, 2014 at 8:45 AM, cynic limbu <span dir="ltr">&lt;<a href="mailto:lists / ruby-forum.com" target="_blank">lists / ruby-forum.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I&#39;m having a problem using daemons (it&#39;s a gem used for running ruby<br>
scripts in the background)<br>
<br>
There are number of tutorials floating on the web but I seriously cannot<br>
understand.<br>
<br>
Can someone shorten it and explain it to me? Thank you.<br>
<br>
I&#39;d like to run the following code in the background<br>
<br>
<br>
Dir.chdir ENV[&#39;HOME&#39;]<br>
if File.exists?(&quot;importantfile.rb&quot;)<br>
nil<br>
else<br>
newfile = File.new(&quot;importantfile.rb&quot;, &quot;w&quot;)<br>
newfile.print(&quot;something&quot;)<br>
newfile.close<br>
end<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Posted via <a href="http://www.ruby-forum.com/" target="_blank">http://www.ruby-forum.com/</a>.<br>
</font></span></blockquote></div><br></div>

--001a1133a8cc28713004f2d93e7f--