# Mongrel PID files and reboot

**URL:** <https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756>\
**Category:** Rails\
**Created:** [April 23, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756 "2007-04-23T22:21:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756/1 "2007-04-23T22:21:55Z")

</div>

I have followed the instructions on the mongrel website, and now have  
mongrel\_cluster running my apps at boot time, works great.

Now, if the server goes down ungracefully, the mongrel processes do  
not start up on reboot because the mongrel PID files are still hanging  
around.

I have boot job that cleans those up now, but is there a “GOOD” way to  
handle this ???

– Thanks

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756/2 "2007-04-23T22:24:55Z")

</div>

Well, that’s how I’ve handled it… I’d also like to know if anyone has  
any  
better methods.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756/3 "2007-04-23T22:37:57Z")

</div>

Actually, I just found this, its looks cleaner than what I have…

[http://textsnippets.com/posts/show/931](http://textsnippets.com/posts/show/931)

Basically just a change to the mongrel\_rails script to delete PID  
files that reference a process that is not actually running…sounds  
like a good thing to be included to me.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756/4 "2007-04-23T22:51:58Z")

</div>

this is what I am using now (changed in mongrel-1.0.1/bin/  
mongrel\_rails  
if File.exist? defaults[:pid\_file]  
#===== new stuff  
pid = File.new(defaults[:pid\_file]).readline  
if `ps --pid #{pid} --no-headers`.length \> 0  
log “!!! PID file #{defaults[:pid\_file]} exists, but  
is stale, and will be deleted so that this mongrel can run.”  
File.delete(defaults[:pid\_file])  
else  
log “!!! PID file #{defaults[:pid\_file]} already  
exists and the process id referred to in it is running. This mongrel  
is probably already running. #{defaults[:log\_file]} for errors.  
EXITING.”  
exit 1  
end  
#===== old stuff  
#log “!!! PID file #{defaults[:pid\_file]} already exists.  
Mongrel could be running already. Check your #{defaults[:log\_file]}  
for errors.”  
#log “!!! Exiting with error. You must stop mongrel and  
clear the .pid before I’ll attempt a start.”  
exit 1  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 23, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756/5 "2007-04-23T23:09:00Z")

</div>

oops, this is actually what I am running now (again patched into /usr/  
lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel\_rails)

```
      if File.exist? defaults[:pid_file]
        #===== new stuff
        pid = File.new(defaults[:pid_file]).readline
        if `ps --pid #{pid} --no-headers`.length > 0
            log "!!! PID file #{defaults[:pid_file]} already

```

exists and the process id referenced is running. This mongrel is  
probably already running. #{defaults[:log\_file]} for errors.  
EXITING."  
exit 1  
else  
log “!!! PID file #{defaults[:pid\_file]} exists, but  
is stale, and will be deleted so that this mongrel can run.”  
File.delete(defaults[:pid\_file])  
end  
#===== old stuff  
#log “!!! PID file #{defaults[:pid\_file]} already exists.  
Mongrel could be running already. Check your #{defaults[:log\_file]}  
for errors.”  
#log “!!! Exiting with error. You must stop mongrel and  
clear the .pid before I’ll attempt a start.”  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 5, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756/6 "2008-03-05T17:25:50Z")

</div>

Found this thread searching the archives, after running into this same  
problem. Anyone know why this hasn’t been added to mongrel\_cluster trunk  
yet? Very annoying. Or maybe it has been, but just wasn’t in the distro  
I got?

Problem is if your server/mongrels dies ungracefully, then  
/etc/init.d/mongrel\_cluster won’t succesfully start up your mongrels  
again, since there are stale pids.

removed\_email\_address@domain.invalid wrote:

> oops, this is actually what I am running now (again patched into /usr/  
> lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel\_rails)
> 
> ```
> if File.exist? defaults[:pid_file]
> #===== new stuff
> pid = File.new(defaults[:pid_file]).readline
> if `ps --pid #{pid} --no-headers`.length > 0
> log "!!! PID file #{defaults[:pid_file]} already
> 
> ```
> 
> exists and the process id referenced is running. This mongrel is  
> probably already running. #{defaults[:log\_file]} for errors.  
> EXITING."  
> exit 1  
> else  
> log “!!! PID file #{defaults[:pid\_file]} exists, but  
> is stale, and will be deleted so that this mongrel can run.”  
> File.delete(defaults[:pid\_file])  
> end  
> #===== old stuff  
> #log “!!! PID file #{defaults[:pid\_file]} already exists.  
> Mongrel could be running already. Check your #{defaults[:log\_file]}  
> for errors.”  
> #log “!!! Exiting with error. You must stop mongrel and  
> clear the .pid before I’ll attempt a start.”  
> end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 5, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/mongrel-pid-files-and-reboot/92756/7 "2008-03-05T17:50:07Z")

</div>

Okay, I found that, at least in the newest versions of mongrel\_cluster,  
you can supply a --clean argument to have mongrel\_cluster delete any  
leftover pids.

Anyone know how I get this to happen on boot? Right now I’m just linking  
/etc/init.d/mongrel\_cluster to  
mongrel\_cluster\_gem/resources/mongrel\_cluster . Any way short of hacking  
the source (meaning it will break next time I update) to get --clean to  
be the default for mongrel\_cluster start?

Jonathan

Jonathan R. wrote:

> Found this thread searching the archives, after running into this same  
> problem. Anyone know why this hasn’t been added to mongrel\_cluster trunk  
> yet? Very annoying. Or maybe it has been, but just wasn’t in the distro  
> I got?
> 
> Problem is if your server/mongrels dies ungracefully, then  
> /etc/init.d/mongrel\_cluster won’t succesfully start up your mongrels  
> again, since there are stale pids.
> 
> removed\_email\_address@domain.invalid wrote:
> 
> > oops, this is actually what I am running now (again patched into /usr/  
> > lib/ruby/gems/1.8/gems/mongrel-1.0.1/bin/mongrel\_rails)
> > 
> > ```
> > if File.exist? defaults[:pid_file]
> > #===== new stuff
> > pid = File.new(defaults[:pid_file]).readline
> > if `ps --pid #{pid} --no-headers`.length > 0
> > log "!!! PID file #{defaults[:pid_file]} already
> > 
> > ```
> > 
> > exists and the process id referenced is running. This mongrel is  
> > probably already running. #{defaults[:log\_file]} for errors.  
> > EXITING."  
> > exit 1  
> > else  
> > log “!!! PID file #{defaults[:pid\_file]} exists, but  
> > is stale, and will be deleted so that this mongrel can run.”  
> > File.delete(defaults[:pid\_file])  
> > end  
> > #===== old stuff  
> > #log “!!! PID file #{defaults[:pid\_file]} already exists.  
> > Mongrel could be running already. Check your #{defaults[:log\_file]}  
> > for errors.”  
> > #log “!!! Exiting with error. You must stop mongrel and  
> > clear the .pid before I’ll attempt a start.”  
> > end
