[Mongrel] problem restarting mongrel_cluster outside RAILS_ROOT - patch and other option

Matte Edens matte at ruckuswireless.com
Tue Apr 17 00:09:45 EDT 2007

Hey folks.  Sorry for the SUPER long email but if you've been 
experiencing the same problems with restarting your mongrel cluster with 
Capistrano, then I have two solutions that have worked for me and I'm 
pretty sure will for you as well.

I was having trouble restarting my clusters using Capistrano.  I've seen 
this come up before on the mailing list and looking through the archive 
I haven't been able to find a suitable answer or fix.

All my machines are updated to the latest mongrel and mongrel_cluster 
gems.  1.0.1 and respectively.  Running a "cap restart" runs the 
correct command to restart the cluster (edited for brevity) ...

"sudo mongrel_rails cluster::restart -C [valid path to config] --clean"

This works when you are sitting in your rails app root, however, 
Capistrano runs it's commands from the ssh user's home directory.  I ran 
that same command from there and I got this dreaded error output.

/*** begin err output ***/
already stopped port 8000
already stopped port 8001
starting port 8000
!!! PID file log/mongrel.8000.pid already exists.  Mongrel could be 
running already.  Check your log/mongrel.8000.log for errors.
!!! Exiting with error.  You must stop mongrel and clear the .pid before 
I'll attempt a start.
starting port 8001
!!! PID file log/mongrel.8001.pid already exists.  Mongrel could be 
running already.  Check your log/mongrel.8001.log for errors.
!!! Exiting with error.  You must stop mongrel and clear the .pid before 
I'll attempt a start.
/*** end err output ***/

What was brought up earlier [1] was that it appears that mongrel is not 
changing the working directory to the one specified in the config file.  
A patch was submitted [2] but by my reckoning, has not been applied and 

However, I believe the patch mentioned above may not be necessary 
because according to my research, the problem isn't with mongrel at 
all.  It's with mongrel_cluster, no offense Bradley.  :)  I've found two 
issues.  One I believe causes the other.

1) The basic problem is that the "start" and "stop" commands, when they 
are scanning for existing pid files, are not being run from the working 
directory, as specified by the :cwd setting in the mongrel_cluster 
config file.  mongrel_cluster does not use the working directory setting 
until it is past that point and finally calling the mongrel_rails 
command.  Thus, it isn't going to find the pid files if you are also 
susceptible to problem #2.

2) A relative directory :pid_file setting in the mongrel_cluster 
config.  If you're like me, your :pid_file setting is 
"log/mongrel.pid".  Using a relative directory like that is supposed to 
be based on the value of the :cwd setting.  But mongrel_cluster is not 
applying the :cwd setting when parsing the :pid_file setting for it's 
internal pid file variables.


1) The solution to the first problem is to patch 
mongrel_cluster/init.rb.  Add some directory change commands, like the 
"status" command uses.  I've uploaded my patch to Pastie at the address 


2) Don't use relative directories for the pid_file setting.  Once I 
changed to an absolute directory setting of, for example, 
"/www/app/shared/log/mongrel.pid" then mongrel_cluster correctly found 
my pid files.  Solution #1 is NOT needed in this instance.

Both solutions require the user to perform an action but I believe that 
the first solution requires less steps for the end user.  Instead of 
updating ALL of your mongrel_cluster config files, for every single app 
you're running, just update to the patched mongrel_cluster.

I suppose there's a THIRD solution and that's to patch the 
"read_options" function in init.rb.  Lines 28 and 29 need to be updated 
to  prepend @options["cwd"] if @options["pid_file"] or 
@options["log_file"] are relative paths.

Am I off base with all this?  Let me know.  And thanx for reading all 
the way to the end.  :)

matte - matte at silent-e.com

1: <http://rubyforge.org/pipermail/mongrel-users/2007-March/003341.html>
2: <http://rubyforge.org/pipermail/mongrel-users/2007-March/003343.html>

More information about the Mongrel-users mailing list