Forum: Ruby How to use daemons?

B108916be03f8a27b45bb94ca26648d2?d=identicon&s=25 cynic limbu (cyniclimbu)
on 2014-02-20 14:45
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 = File.new("importantfile.rb", "w")
newfile.print("something")
newfile.close
end
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2014-02-20 15:27
(Received via mailing list)
Am 20.02.2014 14:45, schrieb cynic limbu:
> 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.

We really cannot rewrite the complete documentation just for you...
You need to be much more specific what your exact problem is.

The docs (http://daemons.rubyforge.org/) provide several examples.

> I'd like to run the following code in the background
>
> Dir.chdir ENV['HOME']
> if File.exists?("importantfile.rb")
> nil
> else
> newfile = File.new("importantfile.rb", "w")
> newfile.print("something")
> newfile.close
> end

On a side note, I would write this snippet rather like this:

  Dir.chdir(ENV['HOME'])

  filename = "importantfile.rb"
  unless File.exists?(filename)
    File.open(filename, "w") {|file| file.print("something") }
  end

The if with 'nil' for doing nothing is really strange,
using the 'filename' variable makes sure that you open the same
file that you previously checked for existence (you might change
one and not the other), and using File.open with block takes care
of closing the stream.

Regards,
Marcus
B108916be03f8a27b45bb94ca26648d2?d=identicon&s=25 cynic limbu (cyniclimbu)
on 2014-02-20 17:28
> On a side note, I would write this snippet rather like this:
>
>   Dir.chdir(ENV['HOME'])
>
>   filename = "importantfile.rb"
>   unless File.exists?(filename)
>     File.open(filename, "w") {|file| file.print("something") }
>   end

I use random codes just to give an example of what I'm talking about.

On the other hand, What I meant to ask is how do you use daemon to run a
ruby script in the background

The following example didn't work for me

ruby myserver_control.rb start

I get:
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/daemonize.rb:11:in
 `fork': fork() function is unimplemented on this machine
(NotImplementedError)
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/daemo
nize.rb:11:in `safefork'
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/daemo
nize.rb:93:in `daemonize'
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/appli
cation.rb:146:in `start_load'
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/appli
cation.rb:298:in `start'
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/contr
oller.rb:70:in `run'
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons.rb:14
7:in `block in run'
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/cmdli
ne.rb:109:in `call'
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/cmdli
ne.rb:109:in `catch_exceptions'
        from
C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons.rb:14
6:in `run'
        from aemons.rb:3:in `<main>'
C0505145a4657b750bbcd2930ac29feb?d=identicon&s=25 David Bahar (Guest)
on 2014-02-20 17:46
(Received via mailing list)
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 = fork 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 = 1".

filename is 'script.god'

God.watch do |w|
  w.name = "script"
  w.dir = "/Users/me/ruby/"
  w.pid_file = w.pid_file = File.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!
C0505145a4657b750bbcd2930ac29feb?d=identicon&s=25 David Bahar (Guest)
on 2014-02-20 17:53
(Received via mailing list)
So you are using Windows - I googled the error you got and found this:

"fork is a POSIX/Unix system call. It is therefore only available on

POSIX/Unix. This does not only apply to Ruby 1.8.6, but to all
versions of Ruby, and it not only applies to Windows, but to all
non-POSIX platforms including, for example, the JVM, the CLI, the
Flash VM or ECMAScript."

So it seems that to gain access to 'fork' you might want to look into
something like cygwin: http://www.cygwin.com/ or try running your code
on a
VM running Linux.
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2014-02-20 18:37
(Received via mailing list)
Am 20.02.2014 17:28, schrieb cynic limbu:
> I use random codes just to give an example of what I'm talking about.

But your example had nothing to do with daemons at all.

> (NotImplementedError)
Good, an error message is much more useful to us.

This is specific to Windows which does not provide forks, I fear
I cannot help. You need to find out whether the daemons gem even
can be used on Windows or not, probably the docs can help.

There is an entire Ruby Rogues episode on daemons, you might find
some useful information there:

  http://rubyrogues.com/122-rr-daemons-with-kenneth-kalmer/

Regards,
Marcus
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (raubarede)
on 2014-02-20 18:42
cynic limbu wrote in post #1137366:

> C:/Ruby200/lib/ruby/gems/2.0.0/gems/daemons-1.1.9/lib/daemons/daemonize.rb:11:in

on windows ...?
use win32-service

or run:
 hstart.exe /noconsole "command ...."
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.