Register now and start sharing your code snippets.
-->

How to get monit to start mongrel_rails properly

Shell Script (Bash) posted 16 days ago by christian

Mongrel_rails and monit are not the best of friends. It’s difficult to get them to work together.

For example, this is the error I got in my monit logs when switching to a new mongrel_rails command that cleans up stale pids:

   1  'mongrel_1' process is not running
   2  'mongrel_2' trying to restart
   3  'mongrel_3' start: /usr/local/bin/mongrel_rails
   4  'mongrel_4' failed to start

To fix it I added the following start_command to the monit configuration:

   1  /usr/bin/env PATH=/usr/local/bin/:$PATH mongrel_rails cluster::start -C /var/www.... --clean --only 8000

The problem is that monit overrides the PATH environment variable, so it won’t find mongrel_rails unless you tell it where to find it.
Monit also contains a bug which doesn’t tell you why it can’t start mongrel_rails, but that’s another story…

Note that I’m using the —clean switch which will startup the mongrels even if a stale pid exists.

In fact I got so tired of the whole mess I wrote a plugin that generates a working monit configuration for mongrel_rails from one or more mongrel_cluster.yml configuration files.

Tagged mongrel_rails, monit, process, monitoring

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

Ruby posted 5 months ago by christian

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

First you’ll need the daemons gem:

   1  gem install daemons

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

   1  require 'rubygems'
   2  require 'daemons'
   3  
   4  pwd  = File.dirname(File.expand_path(__FILE__))
   5  file = pwd + '/../lib/background_service.rb'
   6  
   7  Daemons.run_proc(
   8    'background_service', # name of daemon
   9  #  :dir_mode => :normal
  10  #  :dir => File.join(pwd, 'tmp/pids'), # directory where pid file will be stored
  11  #  :backtrace => true,
  12  #  :monitor => true,
  13    :log_output => true
  14  ) do
  15    exec "ruby #{file}"
  16  end

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:

   1  ERROR: no command given
   2  
   3  Usage: lib/background_service.rb <command> <options> -- <application options>
   4  
   5  * where <command> is one of:
   6    start         start an instance of the application
   7    stop          stop all instances of the application
   8    restart       stop all instances and restart them afterwards
   9    run           start the application and stay on top
  10    zap           set the application to a stopped state
  11  
  12  * and where <options> may contain several of the following:
  13  
  14      -t, --ontop                      Stay on top (does not daemonize)
  15      -f, --force                      Force operation
  16  
  17  Common options:
  18      -h, --help                       Show this message
  19          --version                    Show version

Tagged ruby, daemons, daemon, process, background