[Mongrel] Why not ignore stale PID files?

Gunnar Wolf gwolf at gwolf.org
Fri Jun 6 17:13:10 EDT 2008

Zed A. Shaw dijo [Fri, Jun 06, 2008 at 01:01:32AM -0400]:
> That would be the ideal situation, but Ruby doesn't have good enough
> process management APIs to do this portably.  To make it work you'd
> have to portably be able to take a PID and see if there's a mongrel
> running with that PID.
> You can't use /proc or /sys because that's linux only.  You can't use
> `ps` because the OSX morons changed everything, Solaris has different
> format, etc.
> If you were to do this, you'd have to dip into C code to pull it off.
> Now, if you're only on linux then you could write yourself a small
> little hack to the mongrel_rails script that did this with info out
> of /proc.

Oh, silly me... I thought Ruby's Process class did with the
architectural incompatibilities... What I wrote to check for the
status is quite straightforward:

    require 'yaml'
    confdir = '/etc/mongrel-cluster/sites-enabled'
    restart_cmd = '/etc/init.d/mongrel-cluster restart'
    needs_restart = false
    (Dir.open(confdir).entries - ['.', '..']).each do |site|
      conf = YAML.load_file "#{confdir}/#{site}"
      pid_location = [conf['cwd'],
      conf['pid_file']].join('/').gsub(/\.pid$/, '*.pid')
      pid_files = Dir.glob(pid_location)
      pid_files.each do |pidf|
        pid = File.read(pidf)
        rescue Errno::ESRCH
          warn "Process #{pid} (cluster #{site}) is dead!"
          File.unlink pidf
          needs_restart = true
    system(restart_cmd) if needs_restart

(periodically run via cron)

I guess this works in any Unixy environment... I have no idea on
whether Windows implements something similar to Process.getpgid, or
for that matter, anything on Windows' process management.


Gunnar Wolf - gwolf at gwolf.org - (+52-55)5623-0154 / 1451-2244
PGP key 1024D/8BB527AF 2001-10-23
Fingerprint: 0C79 D2D1 2C4E 9CE4 5973  F800 D80E F35A 8BB5 27AF

More information about the Mongrel-users mailing list