daemons snippets

How to create a daemon process using Ruby and the daemons RubyGem

Tagged ruby, daemons, daemon, process, background  Languages ruby

This snippets shows you how to create a daemon process out of an ordinary Ruby script.

First you'll need the daemons gem:

gem install daemons

Then you'll need the daemon script, for example daemon.rb:

require 'rubygems'
require 'daemons'

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

  'background_service', # name of daemon
#  :dir_mode => :normal
#  :dir => File.join(pwd, 'tmp/pids'), # directory where pid file will be stored
#  :backtrace => true,
#  :monitor => true,
  :log_output => true
) do
  exec "ruby #{file}"

Change the file variable to point to the script you want to daemonize and your good to go.

You can now execute the daemon.rb script without parameters to get a list of available commands for controlling the daemon process:

ERROR: no command given

Usage: lib/background_service.rb <command> <options> -- <application options>

* where <command> is one of:
  start         start an instance of the application
  stop          stop all instances of the application
  restart       stop all instances and restart them afterwards
  run           start the application and stay on top
  zap           set the application to a stopped state

* and where <options> may contain several of the following:

    -t, --ontop                      Stay on top (does not daemonize)
    -f, --force                      Force operation

Common options:
    -h, --help                       Show this message
        --version                    Show version

Daemonization Best-Practices

Tagged daemon, daemons, daemonization, upstart  Languages 

Don't daemonize your daemons

What’s the result? In development mode, your process will run in the foreground, as yourself and log to stdout: perfect for developers. In production mode, the init system will run your process as a configured user with logging sent to a specific location and log rotated automatically. Less system administration, easier debugging, simpler code, all because you leveraged the init system to do the work for you!

Running processes

Note: In every case, it’s assumed that you have a program that wants to run that does not daemonize on its own. Self-daemonizing programs start you down the path to hell. You can’t use any sane keepalive techniques so you have to resort to polling process lists or checking the pid or something. Even managing that pidfile gets hard when you combine it with things that change their own uid for safety (because you should never run anything as root).

Ubuntu, upstart, and creating a pid for monitoring

If start-stop-daemon is available on your machine, I would highly recommend using it to launch your process. start-stop-daemon will handle launching the process as an unprivileged user without forking from sudo or su (recommended in the upstart cookbook) AND it also has built in support for pid file management.

