Skip to content

Instantly share code, notes, and snippets.

@sergio-fry
Forked from jsierles/gist:29838
Created February 2, 2011 15:38

Revisions

  1. @jsierles2 jsierles2 revised this gist Nov 28, 2008. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    # required for passenger since cron has no environment
    ENV['HTTPD'] = 'httpd'

    MEM_LIMIT = ARGV[0].to_i || 500
    MEM_LIMIT = ARGV[0] || 500

    module Process
    def self.running?(pid)
    @@ -23,7 +23,7 @@ def self.running?(pid)
    parts = line.split
    pid, private_dirty_rss = parts[0].to_i, parts[4].to_f

    if private_dirty_rss > MEM_LIMIT
    if private_dirty_rss > MEM_LIMIT.to_i
    puts "Found bloater #{pid} with size #{private_dirty_rss.to_s}"
    puts "Killing with SIGUSR1 (graceful)..."
    Process.kill("SIGUSR1", pid)
  2. jsierles revised this gist Nov 28, 2008. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,9 @@
    # Find bloating passengers and kill them gracefully. Run from cron every minute.
    #

    # required for passenger since cron has no environment
    ENV['HTTPD'] = 'httpd'

    MEM_LIMIT = ARGV[0].to_i || 500

    module Process
  3. jsierles created this gist Nov 27, 2008.
    37 changes: 37 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/usr/bin/env ruby
    #
    # Find bloating passengers and kill them gracefully. Run from cron every minute.
    #

    MEM_LIMIT = ARGV[0].to_i || 500

    module Process
    def self.running?(pid)
    begin
    return Process.getpgid(pid) != -1
    rescue Errno::ESRCH
    return false
    end
    end
    end

    `passenger-memory-stats`.each_line do |line|
    if line =~ /Rails: /
    parts = line.split
    pid, private_dirty_rss = parts[0].to_i, parts[4].to_f

    if private_dirty_rss > MEM_LIMIT
    puts "Found bloater #{pid} with size #{private_dirty_rss.to_s}"
    puts "Killing with SIGUSR1 (graceful)..."
    Process.kill("SIGUSR1", pid)
    puts "Finished kill attempt. Sleeping for 8 seconds..."
    sleep 8
    if Process.running?(pid)
    puts "Process is still running, so killing with extreme predjudice!"
    Process.kill("TERM", pid)
    end
    puts "Done!"
    end

    end
    end