monit, mongrel_rails and ENV["HOME"]

So your mongrels are humming along happily and you have monit monitoring them with a definition like this:

check process mongrel_8010 with pidfile /path/to/current/log/
  start program = "/usr/bin/mongrel_rails cluster::start -C /path/to/current/config/mongrel_cluster.yml --clean --only 8010"
  stop program = "/usr/bin/mongrel_rails cluster::stop -C /path/to/current/config/mongrel_cluster.yml --clean --only 8010"

  if failed host port 8010
    with timeout 10 seconds
    then restart

  if totalmem > 128 Mb then restart
  if cpu is greater than 60% for 2 cycles then alert
  if 3 restarts within 5 cycles then timeout
  group mongrel

The start/stop commands work perfectly from the command line, but somehow not when monit’s calling them. Sure enough, you find this in the mongrel.8010.log file:

/usr/lib/ruby/gems/1.8/gems/rubyforge-1.0.3/lib/rubyforge.rb:15:in `expand_path': couldn't find HOME environment -- expanding `~' (ArgumentError)

The line in question is

  HOME        = ENV["HOME"] || ENV["HOMEPATH"] || File::expand_path("~")

Monit does not set a HOME environment variable, nor HOMEPATH.

The documentation for File::expand_path says:

Converts a pathname to an absolute pathname. Relative paths are referenced from the 
current working directory of the process unless dir_string is given, in which case it 
will be used as the starting point. The given pathname may start with a ``~’’, which 
expands to the process owner‘s home directory (the environment variable HOME must 
be set correctly). ``~user’’ expands to the named user‘s home directory. 

Ouch. So if HOME is not set, File::expand_path(“~”) is guaranteed to fail. That’s a bug in the rubyforge gem I think.

I worked around this by setting ENV["HOME"] to a fallback value before the

require 'rubygems'

line in /usr/bin/mongrel_rails.

I filed a bug. It took me a while to figure out that the rubyforge gem is part of the codeforpeople project on rubyforge.

This entry was posted in Rails and tagged , , , , , , . Bookmark the permalink.

Leave a Reply