Skip to content

Instantly share code, notes, and snippets.

@wmoxam
Created December 30, 2008 19:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save wmoxam/41713 to your computer and use it in GitHub Desktop.
Save wmoxam/41713 to your computer and use it in GitHub Desktop.
#!/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