Skip to content

Instantly share code, notes, and snippets.

@base10
Forked from ntalbott/memory_minder.rb
Created February 22, 2009 02:19
Show Gist options
  • Select an option

  • Save base10/68297 to your computer and use it in GitHub Desktop.

Select an option

Save base10/68297 to your computer and use it in GitHub Desktop.

Revisions

  1. @ntalbott ntalbott revised this gist Feb 20, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions memory_minder.rb
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env ruby

    command = '/opt/ruby-enterprise-1.8.6-20080810/bin/passenger-memory-stats'
    command = '/usr/bin/passenger-memory-stats'
    memory_limit = 200 # megabytes

    def running?(pid)
    @@ -24,4 +24,4 @@ def running?(pid)
    Process.kill("TERM", pid.to_i)
    end
    end
    end
    end
  2. @ntalbott ntalbott renamed this gist Feb 20, 2009. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. wmoxam created this gist Dec 30, 2008.
    27 changes: 27 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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("TERM", pid.to_i)
    end
    end
    end