Skip to content

Instantly share code, notes, and snippets.

@kascote
Forked from wmoxam/gist:41713
Created April 14, 2010 22:46
Show Gist options
  • Save kascote/366441 to your computer and use it in GitHub Desktop.
Save kascote/366441 to your computer and use it in GitHub Desktop.
passenger process monitor
#!/usr/bin/env ruby
command = '/opt/ruby-enterprise-1.8.6-20080810/bin/passenger-memory-stats'
memory_limit = 200 # megabytes
def running?(pid)
begin
return Process.getpgid(pid) != -1
rescue Errno::ESRCH
return false
end
end
`#{command}`.each_line do |line|
next unless /(\d+)\s+\d+\s+(\d+\.\d+)\s+MB\s+(\d+\.\d+)\s+MB\s+Rails:/.match(line)
all, pid, vm_size, private = $~.to_a
if private.to_i > memory_limit
puts "#{Time.now}: Killing #{pid}, memory usage == #{private}"
Process.kill("SIGUSR1", pid.to_i)
puts "Finished kill attempt. Sleeping for 20 seconds..."
sleep 20
if running?(pid.to_i)
puts "Process is still running, sending term signal"
Process.kill("KILL", pid.to_i)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment