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
When Do I Need to Restart Server in Rails
How to Use "Rvm --Default" on Macosx
Ruby: Class Instance Variables VS Instance Variables
Ruby: Installing Rmagick on Ubuntu
Confirmation About Pgrep Returning Itself
Converting Ruby Array to Array of Consecutive Pairs
Rails Attachments Inline Are Not Shown Correctly in Gmail
Is the Server Running on Host "Localhost" (::1) and Accepting Tcp/Ip Connections on Port 5432
Get Current Stack Trace in Ruby Without Raising an Exception
How to Convert Json to a Ruby Hash
Rails 3.1 Limit User Created Objects
Storing Objects in a Session in Rails
How to Run Untrusted Ruby Code Inside a Safe Sandbox
Rails Sends 0 Byte Files Using Send_File
Split the String to Get Only the First 5 Characters
Is There a Hook Similar to Class#Inherited That's Triggered Only After a Ruby Class Definition