wmoxam (owner)

Forks

Revisions

gist: 41713 Download_button fork
public
Public Clone URL: git://gist.github.com/41713.git
Embed All Files: show embed
Text only #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/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