How To Integrate Ruby Daemons

How To Integrate Ruby Daemons

03 Oct 2022
How To Integrate Ruby Daemons

Ruby Daemon:

Ruby Daemons provide an easy way to wrap existing Ruby scripts or records to be run as a daemon process. They can be controlled by simple start/stop/restart commands. They also offer many advanced features like exception backtracking, monitoring, and automatic restarting of the processes if it crashes. In Rails applications, Ruby Daemons are used to run background tasks.

Installation and Generation:

For creating a Daemon for a Rails application, the first step is to install daemons-rails gem as mentioned:

gem 'daemons-rails'

Run the bundle install command on terminal:

bundle install


After successful bundle installation, the next step is to generate daemon:

rails generate daemon <daemon_name>


The above command generates daemon_name.rb and daemon_name_ctl file in the lib/daemons/ directory along with the log file and pids in the general log folder.


Control commands of daemons include:

  • bundle exec rake daemon:daemon_name:start

  • bundle exec rake daemon:daemon_name:stop

  • bundle exec rake daemon:daemon_name:restart

  • bundle exec rake daemon:daemon_name:status


or the following commands can be utilized to start and stop the daemons:

  • bundle exec rake daemons:start

  • bundle exec rake daemons:stop


Monitoring APIs for Daemons:


- hash with all daemons and corresponding statuses


- start daemon using lib/daemons/daemon_name_ctl start

Daemons::Rails::Monitoring.stop("daemon_name .rb")

- start daemon using lib/daemons/daemon_name_ctl stop


- list of controllers


- controller for daemon_name.rb application


ctr = Daemons::Rails::Monitoring.controller("daemon_name.rb")

ctr.path # => lib/daemons/daemon_name_ctl

ctr.app_name # => daemon_name.rb

ctr.start # => starts daemon

ctr.stop # => stops daemon

ctr.status # => :not_exists or :running



To declare default settings for daemons, mention the setting in the config/daemons file. Setting can also be mentioned for a specific daemon in file config/daemon_name-daemon.yml.

Likewise, to use a custom directory instead of default directory lib/daemons, it is important to mention the following block of code in application.rb or an initializer file.


class MyApp < Rails::Application

Daemons::Rails.configure do |config|

config.daemons_path = Rails.root.join("daemons_path")




Example: Consider the following daemon:order with generated files order.rb and order_ctl-






As specified in the above-mentioned files, we can build daemons to make the Rails application more modular and agile. As the daemon's processes run in the background, this will help to balance the load on the Rails application.


Leave a comment: