Skip to content

Instantly share code, notes, and snippets.

@adamcooke
Forked from wmoxam/gist:41713
Created October 19, 2009 18:59
Show Gist options
  • Save adamcooke/213628 to your computer and use it in GitHub Desktop.
Save adamcooke/213628 to your computer and use it in GitHub Desktop.

Revisions

  1. adamcooke revised this gist Oct 19, 2009. 1 changed file with 1 addition and 7 deletions.
    8 changes: 1 addition & 7 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,7 @@
    require 'rubygems'
    require 'daemons'


    options = {}
    options[:log_output] = true
    options[:dir] = File.expand_path('../../log/', __FILE__)
    options[:dir_mode] = :normal

    Daemons.run_proc('PassengerMonitor', options) do
    Daemons.run_proc('PassengerMonitor') do

    command = 'sudo passenger-memory-stats'
    memory_limit = 250
  2. adamcooke revised this gist Oct 19, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -38,8 +38,8 @@ Daemons.run_proc('PassengerMonitor', options) do
    else
    puts "Process ##{pid} is not above limit (#{private}MB)"
    end
    sleep 120
    end
    sleep 120
    end

    end
  3. adamcooke revised this gist Oct 19, 2009. 1 changed file with 44 additions and 26 deletions.
    70 changes: 44 additions & 26 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,45 @@
    #!/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
    #!/usr/bin/env ruby

    require 'rubygems'
    require 'daemons'


    options = {}
    options[:log_output] = true
    options[:dir] = File.expand_path('../../log/', __FILE__)
    options[:dir_mode] = :normal

    Daemons.run_proc('PassengerMonitor', options) do

    command = 'sudo passenger-memory-stats'
    memory_limit = 250

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

    loop do
    `#{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 to give it time to die..."
    sleep 20
    if running?(pid.to_i)
    puts "Process is still running - die bitch"
    Process.kill("KILL", pid.to_i)
    end
    else
    puts "Process ##{pid} is not above limit (#{private}MB)"
    end
    sleep 120
    end
    end

    end
  4. wmoxam revised this gist Mar 5, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ end
    sleep 20
    if running?(pid.to_i)
    puts "Process is still running, sending term signal"
    Process.kill("TERM", pid.to_i)
    Process.kill("KILL", pid.to_i)
    end
    end
    end
  5. 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