Make a Ruby Program a Daemon

Make a Ruby program a daemon?

Use Daemonize.rb

require 'daemons'
Daemons.daemonize

Very simple sample: http://github.com/utkarsh2012/backitup/blob/master/backitup.rb

How to install daemons gem:

gem install daemons

Ruby script as service

I've actually found a much better way of doing that by using ruby scripts.

This is how I did it:

First of all, I installed daemon

gem install daemons

Then I did:

require 'rubygems'
require 'daemons'

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

Daemons.run_proc(
'my_project', # name of daemon
:log_output => true
) do
exec "ruby #{file}"
end

I then create a file called runner.rb, in which I can call my scripts such as:

require "/var/www/rails/my_project/config/environment"
Post.send('details....')

Daemons is a great gem!

Ruby Process.daemon: turning on/off

Typically, the PID is stored in a file that is then read to stop it.

Calling Process.kill(9,Process.pid) kills the "stopper" process itself, rather than the one it's trying to stop.

Here's a guide to writing daemons in Ruby: http://codeincomplete.com/posts/2014/9/15/ruby_daemons/

As you can see, it's not a trivial process.

Here is another blog that suggests that you should not try to daemonize at all, but instead rely on a process monitoring system to take care of those concerns: https://www.mikeperham.com/2014/09/22/dont-daemonize-your-daemons/

Running a Ruby Program as a Windows Service?

Check out the following library: Win32Utils. You can create a simple service that you can start/stop/restart at your leisure. I'm currently using it to manage a Mongrel instance for a Windows hosted Rails app and it works flawlessly.



Related Topics



Leave a reply



Submit